https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61597
--- Comment #21 from Andrew Pinski <pinskia at gcc dot gnu.org> --- The trunk can detect this with the reduced testcase in comment #16 with -fsanitize=address (it could not in GCC 12 though): ================================================================= ==1==ERROR: AddressSanitizer: stack-use-after-return on address 0x7fed23a00020 at pc 0x000000401410 bp 0x7ffd3ef98df0 sp 0x7ffd3ef98de8 READ of size 4 at 0x7fed23a00020 thread T0 #0 0x40140f in main /app/example.cpp:18 #1 0x7fed26088082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId: 1878e6b475720c7c51969e69ab2d276fae6d1dee) #2 0x4010ed in _start (/app/output.s+0x4010ed) (BuildId: eae82a9845e6e2c7baecc615e9a9cf9fe73eac81) Address 0x7fed23a00020 is located in stack of thread T0 at offset 32 in frame #0 0x4011b5 in func(Iter, int) /app/example.cpp:11 This frame has 1 object(s): [32, 36) 'iter' (line 11) <== Memory access at offset 32 is inside this variable HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork (longjmp and C++ exceptions *are* supported) SUMMARY: AddressSanitizer: stack-use-after-return /app/example.cpp:18 in main Shadow bytes around the buggy address: 0x7fed239ffd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7fed239ffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7fed239ffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7fed239fff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7fed239fff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x7fed23a00000: f5 f5 f5 f5[f5]f5 f5 f5 00 00 00 00 00 00 00 00 0x7fed23a00080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7fed23a00100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7fed23a00180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7fed23a00200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x7fed23a00280: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb ==1==ABORTING