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

            Bug ID: 118763
           Summary: [12/13/14/15 regression] memory leak involving early
                    return from statement expressions
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: valentin at tolmer dot fr
  Target Milestone: ---

A "new" with a statement expression returning out of the function still
allocates, but it shouldn't.

source.cpp:
```
int* f() {
  return new int(
      ({
        return nullptr;
        1;
      })
    );
}

int main() { delete f(); }
```

$ g++ -fsanitize=address source.cpp
$ ./a.out
=================================================================
==1==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x7b2e355b2feb in operator new(unsigned long)
(/opt/compiler-explorer/gcc-snapshot/lib64/libasan.so.8+0x126feb) (BuildId:
038c66d3bc17819b861997d7d471707338d4d599)
    #1 0x000000401163 in f() source.cpp:7
    #2 0x000000401173 in main source.cpp:10
    #3 0x7b2e34e29d8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId:
490fef8403240c91833978d494d39e537409b92e)

Note that it's not a matter of calling the constructor:
```
#include <iostream>
struct P {
    P(int i) { std::cout << "construction: " << i << "\n"; }
};
P f() {
  return P(({
        return P(0);
        1;
      }));
}
int main() { f(); }
```
This one will print `construction: 0`.

AFAIU the memory is allocated but never written to (and never freed).

It started with gcc 12.1 (doesn't reproduce with 11.4) and is still happening
on trunk.
https://godbolt.org/z/c7so753z7

Reply via email to