[Bug c++/70180] New: missing -Wpointer-arith on NULL arithmetic cast to a an object type

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Mar 11 03:23:00 GMT 2016


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

            Bug ID: 70180
           Summary: missing -Wpointer-arith on NULL arithmetic cast to a
                    an object type
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

According to the description of the -Wpointer-arith option in the manual:

Warn about anything that depends on the “size of” a function type or of void.
GNU C assigns these types a size of 1, for convenience in calculations with
void * pointers and pointers to functions. In C++, warn also when an arithmetic
operation involves NULL. 

the following program should be diagnosed because it invokes the addition
expression to perform an arithmetic operation involving the NULL pointer. 
However, no diagnostic is issued.  (The cast to int* suppresses the warning.)

Although the example below is wrong (and should be diagnosed), since C++ allows
some arithmetic on null pointers (e.g., adding zero is allowed, as is
subtracting one from another), the implementation should make sure to avoid
diagnosing such expressions (they aren't today) and the description in the
manual should be clarified that they aren't intended to be diagnosed.

$ cat v.c && /home/msebor/build/gcc-trunk-svn/gcc/xgcc
-B/home/msebor/build/gcc-trunk-svn/gcc -S -Wall -Wextra -Wpedantic -Werror
-o/dev/null -xc v.c
#include <stddef.h>

void *p = (int*)NULL + 1;
$


More information about the Gcc-bugs mailing list