From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 69FDF3858414; Sat, 5 Nov 2022 09:58:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69FDF3858414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667642282; bh=mckV/LKAjhGuZ8jAjR7KAxbA3lK5XQ3LHejvmIeXXKM=; h=From:To:Subject:Date:From; b=EmNTiAiYBOoGTn4Htlf+AiDXa60d0XyO2kP8KWYKSLigLJc/oeDWCMLFbQGFM6joN OHd0AZpduOlYYLaLSX96RXsC9OjZiCDqCyU7ylcjQPwGFvPVjqGBo4LW06AfbdUuhI FbzH0Gy9oCGT6rxbrzjkh3lOvsVhPG8rXKheSKT0= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug analyzer/107534] New: Analyzer should flag shallow copies of resources Date: Sat, 05 Nov 2022 09:58:01 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: analyzer X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: dmalcolm at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter blocked target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107534 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 =3D new int(); } ~S() { delete p; } int* p =3D nullptr; }; int main() { S s; S ss =3D s; } Ideally the copy construction of 'ss' would flag that we end up with two ow= ners of the heap pointer. The new object should either make a deep copy (allocat= e 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 construc= tor that makes the shallow copy here). In other words, rather than flagging three symptoms, flag the cause. The ty= pe 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=3D97110 [Bug 97110] [meta-bug] tracker bug for supporting C++ in -fanalyzer=