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

            Bug ID: 128917
           Summary: wrong code: get_range_strlen_phi keeps a nonzero
                    minimum length when a PHI argument has unknown length,
                    so snprintf ("%.5s", p) is assumed >= 4
           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
 gcc -O2 repro.c && ./a.out   (also -O1; OK with -O0 / -fno-printf-return-value
/ clang) */
#include <stdio.h>
#include <string.h>

__attribute__((noipa)) int
f (int a, const char *s, char *q)
{
  memcpy (q, "abcd", 4);           /* q: at least 4 non-nul chars */
  const char *p = a ? s : q;       /* s: unknown string */
  return snprintf (0, 0, "%.5s", p) < 4;
}

int
main (void)
{
  char buf[16] = "xxxxxxxx";
  if (!f (1, "", buf))             /* p = "" -> snprintf returns 0 */
    {
      printf ("FAIL: (snprintf (0, 0, \"%%.5s\", \"\") < 4) folded to 0\n");
      return 1;
    }
  printf ("OK\n");
  return 0;
}
```

Resulted in this output:
```
$ gcc -O2 repro.c && ./a.out
FAIL: (snprintf result range assumed >= 4) (exit 1)
(gcc -O0/-O1, -O2 -fno-optimize-strlen, -O2 -fno-printf-return-value and clang
-O2 exit 0)
```

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

Reply via email to