https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128919
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Kyrylo Tkachov <[email protected]>: https://gcc.gnu.org/g:2326c62a7b0238776b25c3688f2817dcbd8b6382 commit r17-6967-g2326c62a7b0238776b25c3688f2817dcbd8b6382 Author: Kyrylo Tkachov <[email protected]> Date: Tue Nov 24 03:06:05 2026 -0800 strlen: Do not narrow string length bound to a member of a whole-struct pointer [PR128919] v2 to address Sashiko comments. get_maxbound narrows the maximum length of a string to the size of the struct member found at the pointer offset. This was added for -Wformat-overflow (PR104119) to handle MEM [&s, member-offset], the folded form of a member access. The same narrowing applies to a plain &s, which cannot be told apart from &s.first_member. A string written through (char *) &s may legitimately span several members, so the narrowed bound is wrong. In the testcase the PHI <&x, "xy"> gets a maximum length of 3 from the 4-byte member x.a instead of 7. handle_builtin_string_cmp then concludes that the strncmp result is non-zero and records ~[0,0] as its range, and VRP folds the "== 0" test to false. Only narrow when PTR is the address, directly or through its SSA_NAME definition, of an object that has the type of the member found at the offset. That is the case for &s.member and for the folded form MEM <member-type> [&s + member-offset]. It is not the case for &s, for the address of a struct inside another object, or for byte arithmetic on a char pointer, which has the type char. The bound was computed from the pointer stored in the strinfo. That pointer can be the address of an earlier access to the same offset, for example &x.a for a string later written through &x. Use the pointer that is being queried instead. Bootstrapped and tested on aarch64-linux-gnu. gcc/ChangeLog: PR tree-optimization/128919 * tree-ssa-strlen.cc (member_address_p): New function. (get_maxbound): Use it to limit the narrowing to member addresses. (get_range_strlen_dynamic): Pass SRC to get_maxbound instead of the pointer of the strinfo. gcc/testsuite/ChangeLog: PR tree-optimization/128919 * gcc.dg/pr128919.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]>
