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

            Bug ID: 122896
           Summary: -Wdangling-pointer false negative with unconditional
                    use
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: [email protected]
  Target Milestone: ---

alx@devuan:~/tmp$ cat f.c
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

void
f(int x, long *l, int *i, short *s)
{
        if (l == NULL)
                l = &(long){0};
        if (i == NULL)
                i = &(int){0};
        if (s == NULL)
                s = &(short){0};

        *l = 7;

        if (x) *i = 42;

        if (1) *s = 7;
}
alx@devuan:~/tmp$ gcc -S -Wdangling-pointer=1 f.c
alx@devuan:~/tmp$ gcc -S -Wdangling-pointer=2 f.c
f.c: In function ‘f’:
f.c:18:19: warning: dangling pointer ‘i’ to an unnamed temporary may be used
[-Wdangling-pointer=]
   18 |         if (x) *i = 42;
      |                ~~~^~~~
f.c:12:27: note: unnamed temporary defined here
   12 |                 i = &(int){0};
      |                           ^
f.c:20:19: warning: dangling pointer ‘s’ to an unnamed temporary may be used
[-Wdangling-pointer=]
   20 |         if (1) *s = 7;
      |                ~~~^~~
f.c:14:29: note: unnamed temporary defined here
   14 |                 s = &(short){0};
      |                             ^


I see the same exact diagnostics regardless of the optimization level.  I can't
trigger a diagnostic about 'l'.

Reply via email to