public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "buczek at molgen dot mpg.de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/113333] New: analyzer: False positives with calloc()
Date: Thu, 11 Jan 2024 10:01:31 +0000	[thread overview]
Message-ID: <bug-113333-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 113333
           Summary: analyzer: False positives with calloc()
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: buczek at molgen dot mpg.de
  Target Milestone: ---

Analyzer assumen that a pointer allocated by calloc() can be != NULL.


** Code:

#include <stdlib.h>
char **f(void) {
    char **vec = calloc(1, sizeof(char *));
    if (vec)
        for (char **p=vec ; *p ; p++);
    return vec;
}

** Result:

<source>: In function 'f':
<source>:5:29: warning: heap-based buffer over-read [CWE-126]
[-Wanalyzer-out-of-bounds]
    5 |         for (char **p=vec ; *p ; p++);
      |                             ^~
  'f': events 1-6
    |
    |    3 |     char **vec = calloc(1, sizeof(char *));
    |      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                  |
    |      |                  (1) capacity: 8 bytes
    |    4 |     if (vec)
    |      |        ~          
    |      |        |
    |      |        (2) following 'true' branch (when 'vec' is non-NULL)...
    |    5 |         for (char **p=vec ; *p ; p++);
    |      |                     ~       ~~   ~~~
    |      |                     |       |     |
    |      |                     |       |     (5) ...to here
    |      |                     |       (4) following 'true' branch...
    |      |                     |       (6) out-of-bounds read from byte 8
till byte 15 but region ends at byte 8
    |      |                     (3) ...to here
    |
<source>:5:29: note: read of 8 bytes from after the end of the region
    5 |         for (char **p=vec ; *p ; p++);
      |                             ^~
<source>:5:29: warning: use of uninitialized value '*p' [CWE-457]
[-Wanalyzer-use-of-uninitialized-value]
  'f': events 1-6
    |
    |    3 |     char **vec = calloc(1, sizeof(char *));
    |      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                  |
    |      |                  (1) region created on heap here
    |    4 |     if (vec)
    |      |        ~          
    |      |        |
    |      |        (2) following 'true' branch (when 'vec' is non-NULL)...
    |    5 |         for (char **p=vec ; *p ; p++);
    |      |                     ~       ~~   ~~~
    |      |                     |       |     |
    |      |                     |       |     (5) ...to here
    |      |                     |       (4) following 'true' branch...
    |      |                     |       (6) use of uninitialized value '*p'
here
    |      |                     (3) ...to here
    |
Compiler returned: 0

https://gcc.godbolt.org/z/h6bPeYc3T

             reply	other threads:[~2024-01-11 10:01 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11 10:01 buczek at molgen dot mpg.de [this message]
2024-01-11 19:43 ` [Bug analyzer/113333] " dmalcolm at gcc dot gnu.org
2024-01-16  0:02 ` cvs-commit at gcc dot gnu.org
2024-01-16  0:09 ` dmalcolm at gcc dot gnu.org
2024-01-16  8:05 ` buczek at molgen dot mpg.de
2024-04-14  5:04 ` [Bug analyzer/113333] [11/12/13 Regression] " pinskia at gcc dot gnu.org
2024-05-09 17:11 ` cvs-commit at gcc dot gnu.org
2024-05-09 17:52 ` [Bug analyzer/113333] [11/12 " 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-113333-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).