Bug 107534 - Analyzer should flag shallow copies of resources
Summary: Analyzer should flag shallow copies of resources
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: analyzer (show other bugs)
Version: 13.0
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
URL:
Keywords:
Depends on:
Blocks: analyzer-c++
  Show dependency treegraph
 
Reported: 2022-11-05 09:58 UTC by Jonathan Wakely
Modified: 2022-11-05 09:58 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2022-11-05 09:58:01 UTC
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.