public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/94355] support for C++ new expression
Date: Sat, 05 Nov 2022 09:28:14 +0000	[thread overview]
Message-ID: <bug-94355-4-CmmCR6BvNa@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-94355-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> ---
If we add a real bug (making a shallow copy of the object, which shares the
same heap pointer and then both destructors free the same one):

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

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

Then the analyzer still tells us the pointer is leaked:

dest.C: In function ‘int main()’:
dest.C:10:1: warning: leak of ‘s.S::p’ [CWE-401] [-Wanalyzer-malloc-leak]
   10 | }
      | ^
  ‘int main()’: events 1-2
    |
    |    7 | int main() {
    |      |     ^~~~
    |      |     |
    |      |     (1) entry to ‘main’
    |    8 |     S s;
    |      |       ~
    |      |       |
    |      |       (2) calling ‘S::S’ from ‘main’
    |
    +--> ‘S::S()’: events 3-5
           |
           |    2 |     S() { p = new int(); }
           |      |     ^     ~~~~~~~~~~~~~
           |      |     |       |         |
           |      |     |       |         (4) allocated here
           |      |     |       (5) assuming ‘operator new(4)’ is non-NULL
           |      |     (3) entry to ‘S::S’
           |
    <------+
    |
  ‘int main()’: events 6-7
    |
    |    8 |     S s;
    |      |       ^
    |      |       |
    |      |       (6) returning to ‘main’ from ‘S::S’
    |    9 |     S ss = s;
    |   10 | }
    |      | ~      
    |      | |
    |      | (7) ‘s.S::p’ leaks here; was allocated at (4)
    |

But strangely it also tells use there's a use-after-free, which is odd if it
thinks the pointer was nver freed:

dest.C: In destructor ‘S::~S()’:
dest.C:3:22: warning: use after ‘delete’ of ‘s.S::p’ [CWE-416]
[-Wanalyzer-use-after-free]
    3 |     ~S() { delete p; }
      |                      ^
  ‘int main()’: events 1-2
    |
    |    7 | int main() {
    |      |     ^~~~
    |      |     |
    |      |     (1) entry to ‘main’
    |    8 |     S s;
    |      |       ~
    |      |       |
    |      |       (2) calling ‘S::S’ from ‘main’
    |
    +--> ‘S::S()’: events 3-5
           |
           |    2 |     S() { p = new int(); }
           |      |     ^     ~~~~~~~~~~~~~
           |      |     |       |         |
           |      |     |       |         (4) allocated here
           |      |     |       (5) assuming ‘operator new(4)’ is non-NULL
           |      |     (3) entry to ‘S::S’
           |
    <------+
    |
  ‘int main()’: events 6-7
    |
    |    8 |     S s;
    |      |       ^
    |      |       |
    |      |       (6) returning to ‘main’ from ‘S::S’
    |    9 |     S ss = s;
    |   10 | }
    |      | ~      
    |      | |
    |      | (7) calling ‘S::~S’ from ‘main’
    |
    +--> ‘S::~S()’: events 8-11
           |
           |    3 |     ~S() { delete p; }
           |      |     ^      ~~~~~~~~
           |      |     |      |      |
           |      |     |      |      (10) ...to here
           |      |     |      |      (11) deleted here
           |      |     |      (9) following ‘true’ branch...
           |      |     (8) entry to ‘S::~S’
           |
    <------+
    |
  ‘int main()’: events 12-13
    |
    |   10 | }
    |      | ^
    |      | |
    |      | (12) returning to ‘main’ from ‘S::~S’
    |      | (13) calling ‘S::~S’ from ‘main’
    |
    +--> ‘S::~S()’: events 14-17
           |
           |    3 |     ~S() { delete p; }
           |      |     ^      ~~~~~~~~  ~
           |      |     |      |      |  |
           |      |     |      |      |  (17) use after ‘delete’ of
‘*this.S::p’; deleted at (11)
           |      |     |      |      (16) ...to here
           |      |     |      (15) following ‘true’ branch...
           |      |     (14) entry to ‘S::~S’
           |

I think it's (correctly) telling us that the destructor ss.~S() uses the value
after s.~S() already freed it. But it would be nice if it mentioned that the
second destructor is on ss, not s.

The earlier warning tells us the object: "warning: use after ‘delete’ of
‘s.S::p’" 

So it would be nice if the use-after-free was identified as being ss.S::p
(which has the same value as s.S::p).

And it's not actually a use-after-free (and certainly not at the closing brace
of the destructor where the location points to), it's a double-free.

Ideally it would have complained when we copied the pointer value in the
trivial copy constructor and didn't zero out the original. That shallow copy
was the actual bug, the double-free is a symptom of it.


dest.C: In constructor ‘S::S()’:
dest.C:2:13: warning: dereference of possibly-NULL ‘operator new(4)’ [CWE-690]
[-Wanalyzer-possible-null-dereference]
    2 |     S() { p = new int(); }
      |           ~~^~~~~~~~~~~
  ‘S::S()’: events 1-2
    |
    |    2 |     S() { p = new int(); }
    |      |           ~~~~~~~~~~~~^
    |      |             |         |
    |      |             |         (1) this call could return NULL
    |      |             (2) ‘operator new(4)’ could be NULL: unchecked value
from (1)
    |

  parent reply	other threads:[~2022-11-05  9:28 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-27 11:36 [Bug analyzer/94355] New: " vanyacpp at gmail dot com
2020-03-27 11:59 ` [Bug analyzer/94355] " redi at gcc dot gnu.org
2020-09-09 21:24 ` cvs-commit at gcc dot gnu.org
2020-09-09 21:44 ` dmalcolm at gcc dot gnu.org
2020-09-09 21:50 ` dmalcolm at gcc dot gnu.org
2020-09-26  1:35 ` cvs-commit at gcc dot gnu.org
2021-04-12  7:46 ` vanyacpp at gmail dot com
2021-04-12  8:04 ` vanyacpp at gmail dot com
2021-07-22 21:14 ` navarre.gcc.bugs at gmail dot com
2021-07-22 21:35 ` redi at gcc dot gnu.org
2022-11-05  9:21 ` redi at gcc dot gnu.org
2022-11-05  9:28 ` redi at gcc dot gnu.org [this message]
2022-11-05  9:59 ` redi at gcc dot gnu.org
2022-11-05 16:44 ` dmalcolm at gcc dot gnu.org
2022-11-05 21:31 ` redi at gcc dot gnu.org
2023-06-29 20:01 ` [Bug analyzer/94355] analyzer " vultkayn at gcc dot gnu.org
2023-09-01 20:06 ` cvs-commit at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-94355-4-CmmCR6BvNa@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).