https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86296
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
$ g++ b.cpp -O1 -fsantiize=address
$ ./a.out
=================================================================
==22544==ERROR: AddressSanitizer: stack-use-after-scope on address
0x7ffc3624eca0 at pc 0x000000400ec3 bp 0x7ffc3624ebf0 sp 0x7ffc3624ebe8
READ of size 4 at 0x7ffc3624eca0 thread T0
#0 0x400ec2 in main /tmp/b.cpp:74
#1 0x7f45e4d4bf29 in __libc_start_main ../csu/libc-start.c:308
#2 0x400c79 in _start (/tmp/a.out+0x400c79)
Address 0x7ffc3624eca0 is located in stack of thread T0 at offset 160 in frame
#0 0x400d31 in main /tmp/b.cpp:68
This frame has 8 object(s):
[32, 36) 'safe_fd'
[96, 100) '<unknown>'
[160, 164) '<unknown>' <== Memory access at offset 160 is inside this
variable
[224, 228) '__tmp'
[288, 292) '<unknown>'
[352, 356) '<unknown>'
[416, 420) '<unknown>'
[480, 484) '<unknown>'
etc.
Additionally (though this isn't the cause of the behaviour you observe):
$ g++ b.cpp -D_GLIBCXX_ASSERTIONS
$ ./a.out
default initialization: safe_fd =
/home/jwakely/gcc/9/include/c++/9.0.0/bits/unique_ptr.h:328: typename
std::add_lvalue_reference<_Tp>::type std::unique_ptr<_Tp, _Dp>::operator*()
const [with _Tp = int; _Dp = raii_generic_deleter<int, -1, int (*)(int),
close>; typename std::add_lvalue_reference<_Tp>::type = int&]: Assertion 'get()
!= pointer()' failed.
Aborted (core dumped)