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

            Bug ID: 107534
           Summary: Analyzer should flag shallow copies of resources
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
            Blocks: 97110
  Target Milestone: ---

As shown in PR 94355 comment 11, this code reports a use-after-free (and a
spurious leak and spurious null deref):

struct S {
    S() { p = new int(); }
    ~S() { delete p; }
    int* p = nullptr;
};

int main() {
    S s;
    S ss = s;
}

Ideally the copy construction of 'ss' would flag that we end up with two owners
of the heap pointer. The new object should either make a deep copy (allocate a
new object as a copy of *s.p) or should clear the source so it's left empty
(which would require a move constructor, not just the trivial copy constructor
that makes the shallow copy here).

In other words, rather than flagging three symptoms, flag the cause. The type
manages dynamic resources but doesn't have a user-provided copy or move
constructor and so manages them unsafely.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97110
[Bug 97110] [meta-bug] tracker bug for supporting C++ in -fanalyzer

Reply via email to