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

            Bug ID: 122462
           Summary: -fwrapv-pointer suppresses optimization but not
                    -Waddress warning in pointer arithmetic with NULL
           Product: gcc
           Version: 14.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jiangxuezhi2 at huawei dot com
  Target Milestone: ---

I encountered an inconsistency between the behavior of the -fwrapv-pointer
option and the -Waddress warning. The -fwrapv-pointer option changes the
generated assembly code (as expected), but the -Waddress warning is still
issued based on standard C semantics, which seems incorrect when the option is
active.

gcc -O3 -Wall test.c -o test

~~~c
extern char *pNext;
int kkk() {
    return !!(pNext + 1 == (void *)0);
}
~~~

Assembly Output:

kkk:
        xor     eax, eax
        ret

output:
<source>: In function 'kkk':
<source>:4:25: warning: the comparison will always evaluate as 'false' for the
pointer operand in 'pNext + 1' must not be NULL [-Waddress]
    4 |     return !!(pNext + 1 == (void *)0);
      |                         ^~


however,we add option -fwrapv-pointer

Assembly Output:

kkk:
        xor     eax, eax
        cmp     QWORD PTR pNext[rip], -1
        sete    al
        ret

The generated assembly correctly performs a comparison (checking if pNext is
-1), indicating that the pointer wrap-around semantics are respected due to
-fwrapv-pointer.But the output is same as before, like:

<source>: In function 'kkk':
<source>:4:25: warning: the comparison will always evaluate as 'false' for the
pointer operand in 'pNext + 1' must not be NULL [-Waddress]
    4 |     return !!(pNext + 1 == (void *)0);


Additional Information:

This warning occurs due to enhanced -Waddress checks introduced in GCC 12 (as
mentioned in the discussion of PR 102103

Reply via email to