public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/94378] New: -Wanalyzer-malloc-leak false positive when returning a struct by value holding a heap-allocated pointer
@ 2020-03-28  3:08 simon.marchi at polymtl dot ca
  2020-03-29 15:02 ` [Bug analyzer/94378] " marxin at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: simon.marchi at polymtl dot ca @ 2020-03-28  3:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94378
           Summary: -Wanalyzer-malloc-leak false positive when returning a
                    struct by value holding a heap-allocated pointer
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: simon.marchi at polymtl dot ca
  Target Milestone: ---

I tried the analyzer, and I believe it outputs a false positive on this
snippet:

-----

#include <stdlib.h>

struct ret
{
  int *mem;
};

struct ret do_stuff(void)
{
  struct ret r;

  r.mem = malloc(10);

  return r;
}

-----

$ /opt/gcc/git/bin/gcc -c a.c -fanalyzer
a.c: In function ‘do_stuff’:
a.c:14:10: warning: leak of ‘<unknown>’ [CWE-401] [-Wanalyzer-malloc-leak]
   14 |   return r;
      |          ^
  ‘do_stuff’: events 1-2
    |
    |   12 |   r.mem = malloc(10);
    |      |           ^~~~~~~~~~
    |      |           |
    |      |           (1) allocated here
    |   13 | 
    |   14 |   return r;
    |      |          ~ 
    |      |          |
    |      |          (2) ‘<unknown>’ leaks here; was allocated at (1)
    |
a.c:14:10: warning: leak of ‘r.mem’ [CWE-401] [-Wanalyzer-malloc-leak]
   14 |   return r;
      |          ^
  ‘do_stuff’: events 1-3
    |
    |   12 |   r.mem = malloc(10);
    |      |   ~~~~~~~~^~~~~~~~~~
    |      |         | |
    |      |         | (1) allocated here
    |      |         (2) allocated here
    |   13 | 
    |   14 |   return r;
    |      |          ~ 
    |      |          |
    |      |          (3) ‘r.mem’ leaks here; was allocated at (2)
    |

-----

The caller receives the `struct ret` struct by value, and is expected to free
the `mem` field.  I believe the analyzer should not conclude that this is a
leak.

I am on commit 52f24a9e989300506f812bacb8cc302a8bf03a06 (a commit from earlier
today).

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-04-02 15:46 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-28  3:08 [Bug analyzer/94378] New: -Wanalyzer-malloc-leak false positive when returning a struct by value holding a heap-allocated pointer simon.marchi at polymtl dot ca
2020-03-29 15:02 ` [Bug analyzer/94378] " marxin at gcc dot gnu.org
2020-04-01 19:37 ` cvs-commit at gcc dot gnu.org
2020-04-01 19:45 ` dmalcolm at gcc dot gnu.org
2020-04-01 22:21 ` simon.marchi at polymtl dot ca
2020-04-01 22:33 ` dmalcolm at gcc dot gnu.org
2020-04-01 22:38 ` simon.marchi at polymtl dot ca
2020-04-01 23:03 ` dmalcolm at gcc dot gnu.org
2020-04-02 15:46 ` simon.marchi at polymtl dot ca

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).