https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102573
--- Comment #2 from wjf <13508417 at qq dot com> ---
(In reply to Andrew Pinski from comment #1)
> GCC warns:
> <source>: In function 'int main()':
> <source>:9:19: warning: assignment from temporary 'initializer_list' does
> not extend the lifetime of the underlying array [-Winit-list-lifetime]
> 9 | il = {111, 222};
> | ^
>
> Plus at runtime with -fsanitize=address:
>
> =================================================================
> ==1==ERROR: AddressSanitizer: stack-use-after-scope on address
> 0x7ffcff6b4e10 at pc 0x00000040147d bp 0x7ffcff6b4dc0 sp 0x7ffcff6b4db8
> READ of size 4 at 0x7ffcff6b4e10 thread T0
> #0 0x40147c in main /app/example.cpp:13
> #1 0x7f5915fc70b2 in __libc_start_main
> (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
> #2 0x40118d in _start (/app/output.s+0x40118d)
>
> Address 0x7ffcff6b4e10 is located in stack of thread T0 at offset 32 in frame
> #0 0x401255 in main /app/example.cpp:7
>
> This frame has 2 object(s):
> [32, 40) '<unknown>' <== Memory access at offset 32 is inside this
> variable
> [64, 80) 'il' (line 8)
> 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-scope /app/example.cpp:13 in main
> Shadow bytes around the buggy address:
> 0x10001fece970: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> 0x10001fece980: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> 0x10001fece990: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> 0x10001fece9a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> 0x10001fece9b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1
> =>0x10001fece9c0: f1 f1[f8]f2 f2 f2 00 00 f3 f3 00 00 00 00 00 00
> 0x10001fece9d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> 0x10001fece9e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> 0x10001fece9f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> 0x10001fecea00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> 0x10001fecea10: 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
> initializer_list size: 2
> initializer_list underlying elements:
>
>
> clang also errors out the same way.
----
Hi Andrew,
I know that adding "-fsanitize=address" could detect the stack-use-after-scope
in runtime. But why not just delete the copy member in std::initializer_list
implementation? I don't see much sense to permit the copy operation.