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(); } ```