https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115987

--- Comment #21 from Jonathan Wakely <redi at gcc dot gnu.org> ---
If you enable optimization then it does warn for that case:

loc.cc: In function 'int main(int, char**)':
loc.cc:44:9: warning: using a dangling pointer to 'r1' [-Wdangling-pointer=]
   44 |   printf("Unwrapped value : %d\n", u);
      |   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
loc.cc:21:14: note: 'r1' declared here
   21 |   Wrapper<T> r1(r); // << TEMPORARY
      |              ^~
loc.cc:44:9: warning: '*(const int*)((char*)&r1 +
offsetof(Wrapper<int>,Wrapper<int>::val.std::optional<int>::<unnamed>.std::_Optional_base<int,
true, true>::<unnamed>))' is used uninitialized [-Wuninitialized]
   44 |   printf("Unwrapped value : %d\n", u);
      |   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
loc.cc:21:14: note: 'r1' declared here
   21 |   Wrapper<T> r1(r); // << TEMPORARY
      |              ^~


And -fsanitize=address diagnoses it at runtime too:

==2652575==ERROR: AddressSanitizer: stack-use-after-return on address
0x7f79b7609060 at pc 0x0000004013ef bp 0x7ffd9d48e1c0 sp 0x7ffd9d48e1b8

Reply via email to