https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104965
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |msebor at gcc dot gnu.org --- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> --- It looks like an escape analysis limitation. With this simpler test case using different types to rule out aliasing assumptions: #include <string> int main() { std::basic_string<short> s; auto p = new int[s.size ()]{ }; char c = 0; if (s.size()) c = *p; delete[] p; return c; } pr104965.C:9:9: warning: array subscript 0 is outside array bounds of ‘void [0]’ [-Warray-bounds] 9 | c = *p; | ^~ pr104965.C:6:34: note: object of size 0 allocated by ‘operator new []’ 6 | auto p = new short[s.size ()]{ }; | ^ One of the stores to the local s escapes its address which is then assumed to have been clobbered by operator new: <bb 2> [local count: 1073741824]: s ={v} {CLOBBER}; MEM[(struct _Alloc_hider *)&s] ={v} {CLOBBER}; MEM[(struct _Alloc_hider *)&s]._M_p = &s.D.33279._M_local_buf; s._M_string_length = 0; MEM[(char_type &)&s + 16] = 0; _5 = operator new [] (0); <bb 3> [local count: 1073741824]: _10 = s._M_string_length; if (_10 != 0) goto <bb 4>; [50.00%] else goto <bb 5>; [50.00%] <bb 4> [local count: 536870913]: _1 = MEM[(int *)_5]; c_6 = (char) _1;