public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "geoffreydgr at icloud dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug analyzer/109292] New: GCC Static Analyzer NPD false negative because it does not know a simple iterator of `for` loop
Date: Mon, 27 Mar 2023 08:53:03 +0000	[thread overview]
Message-ID: <bug-109292-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 109292
           Summary: GCC Static Analyzer NPD false negative because it does
                    not know a simple iterator of `for` loop
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: geoffreydgr at icloud dot com
  Target Milestone: ---

GCC Static Analyzer has a NPD false negative about `*c = 0;`(line 20).
It seems that GSA does not know the value of the simple iterator `d` of `for`
loop.

If i change the for statement to if statement with the same semantics, there is
no false negative about NPD.

See it live: https://godbolt.org/z/PGdbb6osj
See it live: https://godbolt.org/z/n61zPPxd7

Input
```c
extern void __analyzer_eval();
extern void __analyzer_dump_path();

int a()
{
    int d;
    for (d = -1; d; ++d)
    {
        ;
    }
    __analyzer_dump_path();
    return d;
}

int b()
{
    int t = a();
    int *c = (void *)t;
    __analyzer_eval(c == 0);
    *c = 0;
}

int main() { b(); }
```

Output:
```
<source>: In function 'b':
<source>:18:14: warning: cast to pointer from integer of different size
[-Wint-to-pointer-cast]
   18 |     int *c = (void *)t;
      |              ^
<source>:19:5: warning: UNKNOWN
   19 |     __analyzer_eval(c == 0);
      |     ^~~~~~~~~~~~~~~~~~~~~~~
<source>:19:5: warning: UNKNOWN
<source>: In function 'a':
<source>:11:5: note: path
   11 |     __analyzer_dump_path();
      |     ^~~~~~~~~~~~~~~~~~~~~~
  'a': events 1-3
    |
    |    7 |     for (d = -1; d; ++d)
    |      |                  ^  ~~~
    |      |                  |  |
    |      |                  |  (2) ...to here
    |      |                  (1) following 'true' branch (when 'd != 0')...
    |......
    |   11 |     __analyzer_dump_path();
    |      |     ~~~~~~~~~~~~~~~~~~~~~~
    |      |     |
    |      |     (3) here
    |
Compiler returned: 0
```

                 reply	other threads:[~2023-03-27  8:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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