With just a plain GCC (no options), the code compiled and exhibited the bug (it always printed "okay"). That's to be expected. What was not expected what when I compiled the code with "gcc -ansi -Wall -Wextra -pedantic". NOT ONE ERROR OR WARNING!
How is that happening?
Well, C allows function pointers. Also, in C, a value of 0 used in a pointer context is treated as a NULL pointer. So that one line is seen as:
if (getuid() != 0 && geteuid == NULL)
Now, let's play compiler. geteuid() is a function and it's defined. So the compiler can generate:
if (getuid() != 0 && false)
The result of this expression can never be true. But because the call to getuid() could cause side-effects (the prototype for getuid() does not include the GCC extension marking it as a "pure" function) the compiler has to emit a call to the function, but otherwise, it ignores the result since it doesn't matter what the result is. The resultant code is:
int main(void)
{
getuid();
printf("okay\n");
return 0;
}
And thus, it's possible, even with the commonly used "-Wall -Wextra" options to GCC. To detect this, you will also need to add "-Wunreachable-code" (why that isn't included with '-Wall' is a good question).
> "gcc -ansi -Wall -Wextra -pedantic". NOT ONE ERROR OR WARNING!
$ rpm -q gcc
gcc-4.8.2-7.fc20.x86_64
$ gcc -ansi -Wall -Wextra -pedantic test.c
test.c: In function ‘main’:
test.c:7:36: warning: the comparison will always evaluate as ‘false’ for the address of ‘geteuid’ will never be NULL [-Waddress]
if (getuid() != 0 && geteuid == 0)
^
If fact, just -Wall is enough to enable that warning. Maybe the coverage of -Waddress has changed since your version?
It appears that you are spreading FUD. In a thread discussing if one line bugs could be written on purpose, it is only fitting to discuss if you demonstrated gcc's failing by using a version older than 4.2 was also on purpose.
By the way, I'm interested to know which version you used.
How am I spreading FUD? By using an older version of GCC and reporting what I saw? Should I edit my post to remove all mentions of GCC and just leave how the code is interpreted as is?
It has to be before 4.2 (you never said which version you used), which is really old. And you went to all this trouble but never occurred to you that compiler version could be a factor?
Sure it could be an honest mistake, but that's also true for Apple's bug (and the rest).
External symbols are resolved at link time; as far as the compiler is concerned, setuid might be a symbol declared as __weak.
I don't particularly like the snide remarks towards the maintainers of these libraries.