public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/107289] New: - -Wanayzer-null-dereference false positive with  f = *b
@ 2022-10-17  9:59 geoffreydgr at icloud dot com
  2022-10-17 16:56 ` [Bug analyzer/107289] " dmalcolm at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: geoffreydgr at icloud dot com @ 2022-10-17  9:59 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107289
           Summary: - -Wanayzer-null-dereference false positive with  f =
                    *b
           Product: gcc
           Version: 12.1.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 got a false positive error when compiling the following "minimal, complete
and verifiable example (MCVE)" program

```
int a=1;
int *b = &a;
void c () {
    int f;
    f = *b;
}   
void e(){
    if (0 == b){
        int *g = 0;
    }
}
void d() {  e(); c();}
int main(){
    d();
}
```

Compiling the above code with gcc 12.1 with `-O0 -fanalyzer` in
https://godbolt.org/ results in :

```
<source>: In function 'void c()':
<source>:5:7: warning: dereference of NULL 'b' [CWE-476]
[-Wanalyzer-null-dereference]
    5 |     f = *b;
      |     ~~^~~~
  'void d()': events 1-2
    |
    |   12 | void d() {  e(); c();}
    |      |      ^      ~~~
    |      |      |       |
    |      |      |       (2) calling 'e' from 'd'
    |      |      (1) entry to 'd'
    |
    +--> 'void e()': events 3-6
           |
           |    7 | void e(){
           |      |      ^
           |      |      |
           |      |      (3) entry to 'e'
           |    8 |     if (0 == b){
           |      |     ~~
           |      |     |
           |      |     (4) following 'true' branch...
           |    9 |         int *g = 0;
           |      |              ~
           |      |              |
           |      |              (5) ...to here
           |      |              (6) 'b' is NULL
           |
    <------+
    |
  'void d()': events 7-8
    |
    |   12 | void d() {  e(); c();}
    |      |             ~^~  ~~~
    |      |              |    |
    |      |              |    (8) calling 'c' from 'd'
    |      |              (7) returning to 'd' from 'e'
    |
    +--> 'void c()': events 9-10
           |
           |    3 | void c () {
           |      |      ^
           |      |      |
           |      |      (9) entry to 'c'
           |    4 |     int f;
           |    5 |     f = *b;
           |      |     ~~~~~~
           |      |       |
           |      |       (10) dereference of NULL 'b'
```

It should not be a null pointer deference, because b is a pointer to a. GCC
seems to be fooled by line 8  if (0 == b){...}. But if I change the example
program to the following one, the false positive error disappears:

```
int a=1;
int *b = &a;
void c () {
    int f;
    f = *b;
}   
void d() { c();}
int main(){
    if (0 == b){
        int *g = 0;
    }
    d();
}
```

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

* [Bug analyzer/107289] - -Wanayzer-null-dereference false positive with  f = *b
  2022-10-17  9:59 [Bug analyzer/107289] New: - -Wanayzer-null-dereference false positive with f = *b geoffreydgr at icloud dot com
@ 2022-10-17 16:56 ` dmalcolm at gcc dot gnu.org
  2022-10-17 17:00 ` dmalcolm at gcc dot gnu.org
  2022-10-18  2:45 ` geoffreydgr at icloud dot com
  2 siblings, 0 replies; 4+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2022-10-17 16:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
Thanks for filing this bug; I get the same results with trunk:
  https://godbolt.org/z/3ThE6E5q6

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

* [Bug analyzer/107289] - -Wanayzer-null-dereference false positive with  f = *b
  2022-10-17  9:59 [Bug analyzer/107289] New: - -Wanayzer-null-dereference false positive with f = *b geoffreydgr at icloud dot com
  2022-10-17 16:56 ` [Bug analyzer/107289] " dmalcolm at gcc dot gnu.org
@ 2022-10-17 17:00 ` dmalcolm at gcc dot gnu.org
  2022-10-18  2:45 ` geoffreydgr at icloud dot com
  2 siblings, 0 replies; 4+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2022-10-17 17:00 UTC (permalink / raw)
  To: gcc-bugs

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

David Malcolm <dmalcolm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2022-10-17
     Ever confirmed|0                           |1

--- Comment #2 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
I believe that the analyzer is considering the case where "d" is (somehow)
called from outside of "main", and thus not making the assumption that *b ==
&a; seeing the compare with NULL, it considers the case that b could be NULL.

It's not yet clear to me that this case of d being called from outside "main"
is valid, or if it's always the case that d can only ever be called from main.

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

* [Bug analyzer/107289] - -Wanayzer-null-dereference false positive with  f = *b
  2022-10-17  9:59 [Bug analyzer/107289] New: - -Wanayzer-null-dereference false positive with f = *b geoffreydgr at icloud dot com
  2022-10-17 16:56 ` [Bug analyzer/107289] " dmalcolm at gcc dot gnu.org
  2022-10-17 17:00 ` dmalcolm at gcc dot gnu.org
@ 2022-10-18  2:45 ` geoffreydgr at icloud dot com
  2 siblings, 0 replies; 4+ messages in thread
From: geoffreydgr at icloud dot com @ 2022-10-18  2:45 UTC (permalink / raw)
  To: gcc-bugs

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

Geoffrey <geoffreydgr at icloud dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |geoffreydgr at icloud dot com

--- Comment #3 from Geoffrey <geoffreydgr at icloud dot com> ---
(In reply to David Malcolm from comment #2)
> I believe that the analyzer is considering the case where "d" is (somehow)
> called from outside of "main", and thus not making the assumption that *b ==
> &a; seeing the compare with NULL, it considers the case that b could be NULL.
> 
> It's not yet clear to me that this case of d being called from outside
> "main" is valid, or if it's always the case that d can only ever be called
> from main.

In addition, i changed the original case to the following one (only add
specifier static to `d`) and Compiled it with gcc 12.1 with `-O0 -fanalyzer`

```
int a=1;
int *b = &a;
void c () {
    int f;
    f = *b;
}   
void e (){
    if (0 == b){
        int *g = 0;
    }
}
static void d() {
    e();
    c();
}
int main(){
    d();
}
```

results in:

```
<source>:  In function 'void c()':
<source>:5:7: warning: dereference of NULL 'b' [CWE-476]
[-Wanalyzer-null-dereference]
    5 |     f = *b;
      |     ~~^~~~
  'void d()': events 1-2
    |
    |   12 | static void d() {
    |      |             ^
    |      |             |
    |      |             (1) entry to 'd'
    |   13 |    e();
    |      |    ~~~       
    |      |     |
    |      |     (2) calling 'e' from 'd'
    |
    +--> 'void e()': events 3-6
           |
           |    7 | void e (){
           |      |      ^
           |      |      |
           |      |      (3) entry to 'e'
           |    8 |     if (0 == b){
           |      |     ~~
           |      |     |
           |      |     (4) following 'true' branch...
           |    9 |         int *g = 0;
           |      |              ~
           |      |              |
           |      |              (5) ...to here
           |      |              (6) 'b' is NULL
           |
    <------+
    |
  'void d()': events 7-8
    |
    |   13 |    e();
    |      |    ~^~
    |      |     |
    |      |     (7) returning to 'd' from 'e'
    |   14 |     c();
    |      |     ~~~
    |      |      |
    |      |      (8) calling 'c' from 'd'
    |
    +--> 'void c()': events 9-10
           |
           |    3 | void c () {
           |      |      ^
           |      |      |
           |      |      (9) entry to 'c'
           |    4 |     int f;
           |    5 |     f = *b;
           |      |     ~~~~~~
           |      |       |
           |      |       (10) dereference of NULL 'b'
           |
```
Static analyzer should not give the CWE-476 warning, and it should start
analyzing from function main instead of function `d` because function `d` is a
static function (it is visible only in this compile unit) and only called by
function `main`.
And i am wondering how gcc analyzer handles global variables?

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

end of thread, other threads:[~2022-10-18  2:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-17  9:59 [Bug analyzer/107289] New: - -Wanayzer-null-dereference false positive with f = *b geoffreydgr at icloud dot com
2022-10-17 16:56 ` [Bug analyzer/107289] " dmalcolm at gcc dot gnu.org
2022-10-17 17:00 ` dmalcolm at gcc dot gnu.org
2022-10-18  2:45 ` geoffreydgr at icloud dot com

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