public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/107534] New: Analyzer should flag shallow copies of resources
@ 2022-11-05  9:58 redi at gcc dot gnu.org
  0 siblings, 0 replies; only message in thread
From: redi at gcc dot gnu.org @ 2022-11-05  9:58 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-05  9:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-05  9:58 [Bug analyzer/107534] New: Analyzer should flag shallow copies of resources redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).