(A long story... you can directly jump to the question at the end...)
I need to use realpath(3) so I wrote a simple example to try it:
$> cat realpath.c #include <stdio.h> #include <limits.h> #include <stdlib.h> int main(int argc, char * argv[]) { char pathname[PATH_MAX]; if (realpath(argv[1], pathname) < 0) { perror("realpath"); return 1; } printf("%s\n", pathname); return 0; } $> gcc -Wall -o realpath realpath.c $> ls /xxx ls: cannot access '/xxx': No such file or directory $> ./realpath /xxx/foo/bar /xxx The result of ./realpath /xxx/foo/bar surprised me. According to the manual it makes more sense to fail with ENOENT. I even referred to the POSIX and found no answer. After quite some time I reread the manual and found realpath(3) returns char * rather than int. I was really irritated by gcc.
Question
So why doesn't gcc (even with -Wall) warn about if (ptr < 0)?
0is a perfectly legal null pointer constant. You are comparing the result ofrealpath, which is a pointer, with a null pointer. Not that it is legal, but this violation does not require a diagnostic.if (ptr == 0 /* NULL */)orif (ptr != 0). when does it make sense for> 0or< 0? i believe gcc has a good reason not to warn this by default. no?-std=together with-Wall -Wextra -Wpedantic -Wconversion.