https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91294
Bug ID: 91294 Summary: [10 Regression] wrong strlen result of a conditional with an 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: --- r273783 committed last week to resolve pr91183 introduced a couple of bugs into the strlen pass: when copying the result of a conditional expression involving source strings of different lengths into a destination containing a string, the length of the result isn't computed correctly. Instead of using the length of the shorter of the two source strings (adjusted for any offset into the destination) as the lower bound of the result the optimization use the length of the shorter source (again adjusted for any offset into the destination) as the exact length of the result. The program below includes two test cases, one for each instance of this bug (it's in two places). $ cat z.c && gcc -O2 -Wall z.c && ./a.out const char s[] = "76543210\0\0"; const char t[] = "01234567\0\0"; char a[16], b[16]; int i = 0; int f (void) { __builtin_memcpy (a, i ? s + 2 : t + 1, 8); return __builtin_strlen (a); } int g (void) { __builtin_strcpy (b, "12345678"); __builtin_strcat (b, "9a"); __builtin_memcpy (b + 6, i ? "78\0" : "789\0", 4); return __builtin_strlen (b); } int main (void) { __builtin_printf ("strlen (\"%s\") = %i\n", a, f ()); __builtin_printf ("strlen (\"%s\") = %i\n", b, g ()); } strlen ("1234567") = 6 strlen ("123456789") = 8