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

            Bug ID: 97790
           Summary: constexpr evaluation reports false positive memory
                    leak
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ldalessandro at gmail dot com
  Target Milestone: ---

The constexpr evaluator seems to lose track of dynamic allocations in some
contexts, resulting in false-positive leaks and rejected valid code. 

This particular example uses an immediately invoked function expression
referencing an NTTP, which seems like a corner case but is actually extremely
common in two-pass constexpr algorithms (i.e., compute constexpr size of
result, then compute result to return).

https://godbolt.org/z/Tnn6xh

```
struct vector {
    int *data;
    int n;
    constexpr vector() : data(new int[1]{}), n(1) {}
    constexpr ~vector() { delete [] data; }
};

struct Foo {
    constexpr auto foo() const {
        return vector();
    }
};

template <auto& f>
constexpr static auto bar() {
    [] { return f.foo().n; }();
    return true;
}

constexpr Foo foo;
constexpr auto dd = bar<foo>();
```

Reply via email to