[Bug c++/64767] Could GCC warn when a pointer is compared against '\0'?

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sat Jan 24 15:01:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64767

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2015-01-24
     Ever confirmed|0                           |1
           Severity|enhancement                 |normal

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Confirming as a bug not an extension.

In C++03 the code is valid because pointer conversions are allowed for integral
constant expressions, such as '\0', but in C++11 pointer conversions are only
allowed for an integer-literal and not a char-literal.

So this should be an error in C++11, and adding a warning in C++03 at the same
time would make sense (maybe enabled by -Wzero-as-null-pointer-constant and/or
-Wc++11-compat)

Clang gives a hard error with -std=c++11

warn.cc:4:9: error: comparison between pointer and integer ('void *' and 'int')
  if (p == '\0')
      ~ ^  ~~~~
1 error generated.

Similarly for initializing a pointer with a char-literal, for which clang gives
an error in C++11 mode, and a warning for C++98.

warn.cc:3:9: error: cannot initialize a variable of type 'void *' with an
rvalue of type 'char'
  void* p = '\0';
        ^   ~~~~
1 error generated.


warn.cc:3:13: warning: expression which evaluates to zero treated as a null
pointer constant of type 'void *' [-Wnon-literal-null-conversion]
  void* p = '\0';
            ^~~~
1 warning generated.



More information about the Gcc-bugs mailing list