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/108100] New: GCC Static Analyzer does not know "(a || b) == true" in the true branch of "if (a || b) "
Date: Wed, 14 Dec 2022 12:16:51 +0000	[thread overview]
Message-ID: <bug-108100-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 108100
           Summary: GCC Static Analyzer does not know "(a || b) == true"
                    in the true branch of "if (a || b) "
           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: ---

I found a problem that GCC static analyzer does not know "(a||b) == true" in
the true branch of "if (a || b) ",and takes the 'true' branch of `if (!a)` and
the 'true' branch of `if (!b)` at the same time  (which is contradictory),
resulting in the wrong report of NPD warning. 
I think GCC Static Analyer may not handle '||' operator well. Please take a
look.

I run gcc (trunk) with options `-fanalyzer -O0`
https://godbolt.org/z/4Y41j1GfW

Input:
```c
#include <stdbool.h>

int foo(bool a, bool b) {
    int *c = 0;
    int *d = 0;
    if (a || b){
        __analyzer_eval(a);
        __analyzer_eval(b);
        __analyzer_eval(a||b);
        __analyzer_eval((a||b) == true);

        if (!a){
            if (!b){
                __analyzer_eval(a||b);
                *d = 0;
            }
        }
    }
}

```
Output:
```bash
<source>: In function 'foo':
<source>:7:9: warning: implicit declaration of function '__analyzer_eval'
[-Wimplicit-function-declaration]
    7 |         __analyzer_eval(a);
      |         ^~~~~~~~~~~~~~~
<source>:7:9: warning: UNKNOWN
    7 |         __analyzer_eval(a);
      |         ^~~~~~~~~~~~~~~~~~
<source>:8:9: warning: UNKNOWN
    8 |         __analyzer_eval(b);
      |         ^~~~~~~~~~~~~~~~~~
<source>:9:9: warning: UNKNOWN
    9 |         __analyzer_eval(a||b);
      |         ^~~~~~~~~~~~~~~~~~~~~
<source>:10:9: warning: UNKNOWN
   10 |         __analyzer_eval((a||b) == true);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:14:17: warning: UNKNOWN
   14 |                 __analyzer_eval(a||b);
      |                 ^~~~~~~~~~~~~~~~~~~~~
<source>:15:20: warning: dereference of NULL 'd' [CWE-476]
[-Wanalyzer-null-dereference]
   15 |                 *d = 0;
      |                 ~~~^~~
  'foo': events 1-7
    |
    |    4 |     int *c = 0;
    |      |          ^
    |      |          |
    |      |          (1) 'c' is NULL
    |    5 |     int *d = 0;
    |      |          ~
    |      |          |
    |      |          (2) 'c' is NULL
    |......
    |   12 |         if (!a){
    |      |            ~
    |      |            |
    |      |            (3) following 'true' branch...
    |   13 |             if (!b){
    |      |                ~~~
    |      |                ||
    |      |                |(4) ...to here
    |      |                (5) following 'true' branch...
    |   14 |                 __analyzer_eval(a||b);
    |      |                 ~~~~~~~~~~~~~~~~~~~~~
    |      |                 |
    |      |                 (6) ...to here
    |   15 |                 *d = 0;
    |      |                 ~~~~~~
    |      |                    |
    |      |                    (7) dereference of NULL 'd'
    |
Compiler returned: 0
```

             reply	other threads:[~2022-12-14 12:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-14 12:16 geoffreydgr at icloud dot com [this message]
2023-04-04  1:20 ` [Bug analyzer/108100] " geoffreydgr at icloud dot com

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