Bug 250019

Summary: /usr/src/stand/uboot/lib/glue.c
Product: Base System Reporter: Samee Shahzada <onestsam>
Component: standardsAssignee: freebsd-standards (Nobody) <standards>
Status: Closed Not A Bug    
Severity: Affects Only Me CC: emaste, imp
Priority: ---    
Version: CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
/usr/src/stand/uboot/lib/glue.c none

Description Samee Shahzada 2020-10-01 02:56:53 UTC
Created attachment 218442 [details]
/usr/src/stand/uboot/lib/glue.c

This is my first time submitting a bug to the FreeBSD OS. This is a bug not picked up by compilers and leads to a lot of headaches (for me) in c. Please let me know if this report is in error. 

Please also let me know how to properly report bugs for the OS.
Comment 1 Conrad Meyer freebsd_committer freebsd_triage 2020-10-02 16:22:36 UTC
Hi Samee,

This isn’t a bug and the change does not impact the meaning of the code in any way.
Comment 2 Warner Losh freebsd_committer freebsd_triage 2020-10-02 16:25:47 UTC
Can you explain the headaches?

sizeof(char) == 1, so 1 * 256 is 256 and wouldn't change behavior at all.

What's the bug this is trying to fix?
Comment 3 Samee Shahzada 2020-10-02 17:48:11 UTC
sizeof(char) == 1

and if you:

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
char s1[256];
char s2[256];

memset(s1, 0, 256);
memset(s2, 0, (sizeof(char)*256));

printf("%s", s1);
printf("%s", s2);


        return(0);
}

everything will be fine but if you:

int main(int argc, char **argv) {
char s1[256];
char s2[256];

blah(s1, s2);

        return(0);
}

void blah(char *s1, char *s2) {

memset(s1, 0, 256);
memset(s2, 0, (sizeof(char)*256));

printf("%s", s1);
printf("%s", s2);

}

the one without the sizeof will frequently spit out garbage. 

I dont know why but i think sometimes the compiler mistakes
(char*) for (char).

and even memset((char*)s1, 0, 256) wont fix it.

I think the compiler is doing this:

memset((char*)s2, 0, (sizeof(char*)*256));

but this should result in a core dump and not a buffer filled with garbage.

I dont know. Something to think about.
Comment 4 Ed Maste freebsd_committer freebsd_triage 2020-10-02 19:05:21 UTC
(In reply to Samee Shahzada from comment #3)
Turn on compiler warnings and it will tell you what the problem is:

$ cc -Wall pr250019.c 
pr250019.c:5:1: warning: implicit declaration of function 'blah' is invalid in C99 [-Wimplicit-function-declaration]
blah(s1, s2);
^
pr250019.c:10:6: error: conflicting types for 'blah'
void blah(char *s1, char *s2) {
     ^
pr250019.c:5:1: note: previous implicit declaration is here
blah(s1, s2);
^
pr250019.c:12:1: warning: implicitly declaring library function 'memset' with type 'void *(void *, int, unsigned long)' [-Wimplicit-function-declaration]
memset(s1, 0, 256);
^
pr250019.c:12:1: note: include the header <string.h> or explicitly provide a declaration for 'memset'
pr250019.c:15:1: warning: implicitly declaring library function 'printf' with type 'int (const char *, ...)' [-Wimplicit-function-declaration]
printf("%s", s1);
^
pr250019.c:15:1: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
Comment 5 Samee Shahzada 2020-10-02 19:43:18 UTC
that was the compiler complaining that I didnt first declare blah and need to include stdio. Thats because the code was incomplete.

I was showing you where and how the bug manifests. 

I promise you. 

memset(s1, 0, 256); gonna frequently confuse the compiler and will frequently not memset the buffer correctly. The compiler will not give you a warning and you are going to pull out your hair trying to figure out what's wrong. 

I really think something like memset(s1, 0, (sizeof(char*)*256)); is happening.

Its technically a bug in the compiler and not with FreeBSD but that fact does not put the hair back in your head.
Comment 6 Warner Losh freebsd_committer freebsd_triage 2020-10-02 19:52:47 UTC
> I really think something like memset(s1, 0, (sizeof(char*)*256)); is happening.

No. That's simply not possible. A compiler that randomly screws up simple integer constants is too broken to use. And 1*constant really isn't going to help things much unless it's a super specific bug.

Since there's nothing specific and actionable listed as a root cause, we should reject this submission.
Comment 7 Ed Maste freebsd_committer freebsd_triage 2020-10-02 20:52:35 UTC
(In reply to Samee Shahzada from comment #5)
There is no compiler bug here. If you want to convince yourself, compile your source in "memset(foo, 0, 256)" and "memset(foo, 0, sizeof(char)*256)" versions to two separate object files and compare the results. You will find that they are identical.