https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94355
--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> ---
struct S {
S() { p = new int(); }
~S() { delete p; }
int* p = nullptr;
};
int main() {
S s;
}
Everything the analyzer says is wrong :-)
dest.C: In function ‘int main()’:
dest.C:9:1: warning: leak of ‘s.S::p’ [CWE-401] [-Wanalyzer-malloc-leak]
9 | }
| ^
‘int main()’: events 1-2
|
| 7 | int main() {
| | ^~~~
| | |
| | (1) entry to ‘main’
| 8 | S s;
| | ~
| | |
| | (2) calling ‘S::S’ from ‘main’
|
+--> ‘S::S()’: events 3-5
|
| 2 | S() { p = new int(); }
| | ^ ~~~~~~~~~~~~~
| | | | |
| | | | (4) allocated here
| | | (5) assuming ‘operator new(4)’ is non-NULL
| | (3) entry to ‘S::S’
|
<------+
|
‘int main()’: events 6-7
|
| 8 | S s;
| | ^
| | |
| | (6) returning to ‘main’ from ‘S::S’
| 9 | }
| | ~
| | |
| | (7) ‘s.S::p’ leaks here; was allocated at (4)
|
dest.C: In constructor ‘S::S()’:
dest.C:2:13: warning: dereference of possibly-NULL ‘operator new(4)’ [CWE-690]
[-Wanalyzer-possible-null-dereference]
2 | S() { p = new int(); }
| ~~^~~~~~~~~~~
‘S::S()’: events 1-2
|
| 2 | S() { p = new int(); }
| | ~~~~~~~~~~~~^
| | | |
| | | (1) this call could return NULL
| | (2) ‘operator new(4)’ could be NULL: unchecked value
from (1)
|
As already noted above, new can't return null here, and there is no dereference
anyway. And the pointer isn't leaked, but it seems maybe the analyzer doesn't
know about destructors?