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

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

            Bug ID: 94458
           Summary: -Wanalyzer-malloc-leak false positive when returning a
                    heap-allocated 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: ---

Variation of [1], where the returned struct is heap-allocated, rather than
returned by value.

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

Source:

#include <stdlib.h>

struct ret
{
  int **array;
};

struct ret *allocate_stuff(void)
{
        struct ret *ret;

        ret = calloc(1, sizeof (struct ret));
        if (!ret) {
            abort();
        }

        ret->array = calloc (10, sizeof(int *));
        if (!ret->array) {
            abort();
        }

        return ret;
}

Analyzer report:

$ /opt/gcc/git/bin/gcc a.c -g3 -O0 -fanalyzer -c -Wall -Werror
a.c: In function ‘allocate_stuff’:
a.c:18:10: error: leak of ‘<unknown>’ [CWE-401] [-Werror=analyzer-malloc-leak]
   18 |  if (!ret->array) {
      |       ~~~^~~~~~~
  ‘allocate_stuff’: events 1-7
    |
    |   13 |  if (!ret) {
    |      |     ^
    |      |     |
    |      |     (1) following ‘false’ branch (when ‘ret’ is non-NULL)...
    |......
    |   17 |  ret->array = calloc (10, sizeof(int *));
    |      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |               |
    |      |               (2) ...to here
    |      |               (3) allocated here
    |   18 |  if (!ret->array) {
    |      |     ~ ~~~~~~~~~~
    |      |     |    |
    |      |     |    (7) ‘<unknown>’ leaks here; was allocated at (3)
    |      |     (4) assuming ‘<unknown>’ is non-NULL
    |      |     (5) following ‘false’ branch...
    |......
    |   22 |  return ret;
    |      |         ~~~
    |      |         |
    |      |         (6) ...to here
    |
cc1: all warnings being treated as errors

$ /opt/gcc/git/bin/gcc --version                              
gcc (GCC) 10.0.1 20200401 (experimental)


The analyzer says the memory allocated at (3) is leaked, but it is in fact
pointed by the returned struct.

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

end of thread, other threads:[~2020-08-13 20:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-02 15:46 [Bug analyzer/94458] New: -Wanalyzer-malloc-leak false positive when returning a heap-allocated struct by value holding a heap-allocated pointer simon.marchi at polymtl dot ca
2020-05-08 15:09 ` [Bug analyzer/94458] " dmalcolm at gcc dot gnu.org
2020-06-19  0:55 ` eggert at cs dot ucla.edu
2020-08-13 20:30 ` dmalcolm 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).