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

            Bug ID: 128918
           Summary: wrong code: maybe_duplicate_ssa_info_at_copy copies a
                    range that holds only after a later dereference in the
                    same block to an earlier copy, so a null check is
                    folded away
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 0599jiangyc at gmail dot com
  Target Milestone: ---

The following code:

```c
/* Godbolt: x86-64 gcc (trunk), flags: -O2
 FRE copies inlined-callee range info ([1,+INF], valid only after *p)
   onto an earlier equivalent SSA name in the same block.
   gcc -O2 repro.c && ./a.out    (also -O3, -Os, -O1 -ftree-vrp; GCC 11 too)
   Expected (gcc -O0/-O1, clang -O2): exit 0.  gcc -O2: check() gets 0, *p
   dereferences NULL -> exit 1.  */
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

__attribute__((noipa)) void check (int is_null)
{
  if (is_null)
    exit (0);                   /* p == 0: never reaches *p */
}

static inline uintptr_t h (int *p)
{
  int v = *p;
  uintptr_t i = (uintptr_t) p;  /* evrp in h: i = [1, +INF] */
  return i + v;
}

__attribute__((noipa)) uintptr_t f (int *p)
{
  uintptr_t i1 = (uintptr_t) p; /* fre1 after einline: h's i -> i1, range
copied */
  check (__builtin_popcountl (i1) + (int) (i1 >> 60) == 0);
  return h (p);
}

static void segv (int s) { (void) s; _exit (1); }

int main (void)
{
  int a = 1;
  signal (SIGSEGV, segv);
  if (f (&a) != (uintptr_t) &a + 1)
    return 1;
  f (0);                        /* must exit (0) inside check */
  return 1;
}
```

Resulted in this output:
```
$ gcc -O2 repro.c && ./a.out
(exit 1: f(0) dereferences NULL after check() was folded to check(0))
(gcc -O0/-O1, -O2 -fno-tree-vrp, -O2 -fno-inline and clang -O2 exit 0)
```

Trunk: https://godbolt.org/z/7a4TM33P6 (x86-64 gcc trunk, -O2, executed;
reproduces as of 2026-11-13; also GCC 14.2 and GCC 11)

Reply via email to