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

Drea Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #8 from Drea Pinski <pinskia at gcc dot gnu.org> ---
So the problem is in call for the case of `Index_ == sizeof...(Functors_)`.
There is a return of a copy of the temporary argument happening

If I change it to be this:
```
  template <std::size_t Index_>
  constexpr inline decltype(auto) call(this auto&& c, auto&&... args) {
    if constexpr (Index_ == sizeof...(Functors_)) {
    #if 0
      return std::forward<decltype(args...[0])>(args...[0]);
      #else

      auto t = args...[0];
      return t;
      #endif
    } else {
      return c.template call<Index_ + 1>(
       
c.Functors_...[Index_]::operator()(std::forward<decltype(args)>(args)...)
      );
    }
  }
```

That fixes the issue.  Also note clang shows the same issue (though no
warning).

 -fsanitize=address is also able to detect this lifetime issue too:
ASM generation compiler returned: 0
Execution build compiler returned: 0
Program returned: 1
c(): =================================================================
==2==ERROR: AddressSanitizer: stack-use-after-return on address 0x728b7bef0020
at pc 0x00000040156f bp 0x7ffd98d76d50 sp 0x7ffd98d76d48
READ of size 4 at 0x728b7bef0020 thread T0
    #0 0x00000040156e in main (/app/output.s+0x40156e) (BuildId:
f754f159668c4142c2b7b0327b2dd60832623483)
    #1 0x768b7e02a1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId:
a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #2 0x768b7e02a28a in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId:
a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x000000401134 in _start (/app/output.s+0x401134) (BuildId:
f754f159668c4142c2b7b0327b2dd60832623483)

Address 0x728b7bef0020 is located in stack of thread T0 at offset 32 in frame
    #0 0x00000040128e in decltype(auto) composed<main::{lambda()#1}>::call<0ul,
composed<main::{lambda()#1}>&>(this composed<main::{lambda()#1}>&)
(/app/output.s+0x40128e) (BuildId: f754f159668c4142c2b7b0327b2dd60832623483)

  This frame has 1 object(s):
    [32, 36) '<unknown>' <== 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/output.s+0x40156e)
(BuildId: f754f159668c4142c2b7b0327b2dd60832623483) in main
Shadow bytes around the buggy address:
  0x728b7beefd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x728b7beefe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x728b7beefe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x728b7beeff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x728b7beeff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x728b7bef0000: f5 f5 f5 f5[f5]f5 f5 f5 00 00 00 00 00 00 00 00
  0x728b7bef0080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x728b7bef0100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x728b7bef0180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x728b7bef0200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x728b7bef0280: 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
==2==ABORTING

Reply via email to