https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86572
Bug ID: 86572 Summary: unsafe strlen folding of const arguments with non-const offset Product: gcc Version: 9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- This came up in a conversation of a change in this area (bug 86532). The following test case has undefined behavior so anything can happen, but the result in GCC is unnecessarily and arbitrarily "hostile" in that the value computed by the strlen expression is excessive large, so large in fact as to be meaningless (no object can be as large as 18446744073709551615 or SIZE_MAX bytes). $ cat c.c && gcc -Wall -fdump-tree-gimple=/dev/stdout c.c && ./a.out const char a[] = "123"; __attribute__ ((noipa)) __SIZE_TYPE__ f (int i) { return __builtin_strlen (a + i); } int main (void) { __SIZE_TYPE__ n = f (4); __builtin_printf ("%zu\n", n); } __attribute__((noipa, noinline, noclone, no_icf)) f (int i) { long unsigned int D.1964; _1 = (ssizetype) i; _2 = 3 - _1; // strlen folded into this D.1964 = (long unsigned int) _2; return D.1964; } main () { int D.1966; { long unsigned int n; n = f (4); __builtin_printf ("%zu\n", n); } D.1966 = 0; return D.1966; } 18446744073709551615