public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pinskia at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/94839] New: False positive with -fanalyzer and direct field assignment from calloc
Date: Wed, 29 Apr 2020 02:15:49 +0000	[thread overview]
Message-ID: <bug-94839-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 94839
           Summary: False positive with -fanalyzer and direct field
                    assignment from calloc
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
struct bitmap
{
  int min;
  int max;
  int *vec;
};


int bitmap_create(struct bitmap *bm, int min, int max)
{
        int sz;

        sz = (max / sizeof(int)) + 1;

        bm->min = min;
        bm->max = max;
        bm->vec = __builtin_calloc(sz, sizeof(int));
        if (!bm->vec)
                return (-12);
        return 0;
}

----- CUT ----
This gives (at -O2 -fanalyzer -W -Wall ):
In function ‘bitmap_create’:
t6666_1.c:18:12: warning: leak of ‘<unknown>’ [CWE-401]
[-Wanalyzer-malloc-leak]
   18 |         if (!bm->vec)
      |            ^
  ‘bitmap_create’: events 1-3
    |
    |   13 |         sz = (max / sizeof(int)) + 1;
    |      |              ~~~~~^~~~~~~~~~~~~~
    |      |                   |
    |      |                   (1) allocated here
    |......
    |   18 |         if (!bm->vec)
    |      |            ~
    |      |            |
    |      |            (2) assuming ‘<unknown>’ is non-NULL
    |      |            (3) following ‘false’ branch...
    |
  ‘bitmap_create’: event 4
    |
    |cc1:
    | (4): ...to here
    |
  ‘bitmap_create’: event 5
    |
    |   18 |         if (!bm->vec)
    |      |            ^
    |      |            |
    |      |            (5) ‘<unknown>’ leaks here; was allocated at (1)
    |

Except there is no leaking as assign the calloc to bm->vec .

If we change the type of vec to void*, there is no warning.

             reply	other threads:[~2020-04-29  2:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-29  2:15 pinskia at gcc dot gnu.org [this message]
2020-04-29  2:17 ` [Bug analyzer/94839] " pinskia at gcc dot gnu.org
2020-08-13 20:31 ` dmalcolm at gcc dot gnu.org
2020-08-13 20:34 ` dmalcolm 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-94839-4@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).