https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108217
Bug ID: 108217 Summary: bogus -Warray-bounds with pointer to constant local Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: jhaberman at gmail dot com Target Milestone: --- Repro: void ExternFunc1(); void ExternFunc2(const int*); char mem[32]; static void StaticFunc(const int* i) { void* ptr = (void*)0; switch (*i) { case 0: ExternFunc2(i); return; case 1: __builtin_memcpy(mem, &ptr, sizeof(ptr)); return; case 2: { __builtin_memcpy(mem, &ptr, 32); return; } } } void Bad() { const int i = 1; ExternFunc1(); StaticFunc(&i); } This reproduces on trunk according to Godbolt: https://godbolt.org/z/vYGo1z6bG Godbolt also indicates a missed optimization, which is probably related to the bogus warning. Clang correctly performs constant propagation of the local `i`, whereas GCC seems to think that all cases of the switch() are reachable. It is true that &i escapes, but mutating `i` is UB because it is const, so it should be legal to perform constant propagation here. Additionally, even if ExternFunc2() mutated `i`, it would be too late to change its value in time to affect the switch().