Warning when returning NULL with gcc

Using gcc 5.2.0, I noticed that this code does not generate a warning:

 #include <stddef.h> int function(void) { return NULL; } void procedure(void) { return NULL; } 

I used the flags -Wall -Wextra -std=c99 -pedantic and I am running archlinux. I'm not sure why this code works fine on gcc , especially since clang 3.7.0 generates a warning.

I also tried using older versions of gcc , such as 4.9 or 4.7, and both of them generate warnings.

Warnings:

 warning: return makes integer from pointer without a cast 

and

 warning: 'return' with a value, in function returning void 

I should mention that I tried compiling gcc 5.2 on Debian and the result is the same. So archlinux doesn't seem to be a problem.

What's the point? I can't seem to find anything in this friend.

Thanks!

+6
source share
3 answers

I filled out an error report here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67730

This has been confirmed and fixed for 5.3, it seems!

Thank you for your help.

+1
source

This is apparently a regression in gcc 5.2.0 - and, as far as I can tell, weird.

With gcc 4.4.5 and 4.8.4, the program (after adding the required #include <stddef.h> ) issues warnings in both return NULL; , even without additional command line options.

With gcc 5.2.0, warnings are not issued even with gcc -c -Wall -Wextra -std=c99 -pedantic . For the second, returning the value from the void function, diagnostics is required.

Now here is the weird part.

 $ gcc --version | head -n 1 gcc (GCC) 5.2.0 $ cat cc #include <stddef.h> int function(void) { #ifdef USE_NULL return NULL; #else return ((void*)0); #endif } void procedure(void) { #ifdef USE_NULL return NULL; #else return ((void*)0); #endif } $ gcc -c cc cc: In function 'function': cc:7:12: warning: return makes integer from pointer without a cast [-Wint-conversion] return ((void*)0); ^ cc: In function 'procedure': cc:15:12: warning: 'return' with a value, in function returning void return ((void*)0); ^ $ gcc -c -DUSE_NULL cc $ 

When USE_NULL defined, no warnings are generated. Compiling with gcc -E -DUSE_NULL (which outputs the preprocessor output to stdout) confirms that NULL is defined as ((void*)0) . But when I replace NULL with ((void*)0) , warnings are generated (as it should be).

I do not know what's happening. Once the NULL macro has been expanded, it must be indistinguishable from ((void*)0) , and yet the behavior of the compiler changes depending on which one is used.

+3
source

The validity of the first function depends on how NULL determined. There is no problem with the first function if NULL is defined as 0 . But if it is defined as (void *) 0 (which is usually the case in C code), the first function becomes invalid. GCC is often very authoritative with respect to implicit conversions with a pointer to an integer.

The second function is invalid. The language specification clearly states that "a return statement with an expression must not appear in a function whose return type is invalid." If it compiles, it should be another GCC example that over-permits something.

You need to run GCC in -pedantic mode if you want its diagnostic output to be associated with the standard C language. In default mode, this is not a credible C compiler (at least in the topics of its diagnostic output), and the fact that he "does not display a warning" means absolutely nothing.

However, in my experiments, I could not get GCC to complain about the second function even with the -std= and -pedantic . This seems like a compiler error.

+1
source

All Articles