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

            Bug ID: 119893
           Summary: '-Wnonnull' should not warn null pointer use in
                    'typeof' expression
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Explorer09 at gmail dot com
  Target Milestone: ---

Test code:

```c
__attribute__((nonnull)) \
int func(void *p);

__typeof__(func((void *)0)) func2(void *p);

typeof(func((void *)0)) main(void)
{
    return 0;
}
```

Compiling with `gcc -std=gnu23 -Wnonnull` produces warnings like these:

```text
<source>:4:12: warning: argument 1 null where non-null expected [-Wnonnull]
    4 | __typeof__(func((void *)0)) func2(void *p);
      |            ^~~~
<source>:2:30: note: in a call to function 'func' declared 'nonnull'
    2 | __attribute__((nonnull)) int func(void *p);
      |                              ^~~~
<source>:6:8: warning: argument 1 null where non-null expected [-Wnonnull]
    6 | typeof(func((void *)0)) main(void) {
      |        ^~~~
<source>:2:30: note: in a call to function 'func' declared 'nonnull'
    2 | __attribute__((nonnull)) int func(void *p);
      |                              ^~~~
```

Clang 20 produces no warnings, and I expect GCC produce no warnings either. In
this 'typeof' expression, the function should be never called and the fact I
passed a `(void *)0` there shouldn't matter.

A workaround is to add an offset to null pointer so the address becomes
technically non-zero:

```c
/* Workaround */
__typeof__(func(1 + (void *)0)) func2(void *p);
```

Reply via email to