testnullpointer.cpp: Add test for dereferencing returned NULL pointer (#2401)

This adds a regression test to make sure that directly dereferencing a
returned NULL pointer issues a warning.
This has been asked on Stack Overflow:
https://stackoverflow.com/q/58981369
Cppcheck 1.89 does not warn for such a code, but 1.90 dev does. So it
is a good idea to make sure it is detected in the future too I guess.
This commit is contained in:
Sebastian 2019-11-27 19:41:36 +01:00 committed by amai2012
parent 8af2ee968e
commit fd900ab8b2
1 changed files with 8 additions and 0 deletions

View File

@ -2062,6 +2062,14 @@ private:
" typeof(*NULL) y;\n" " typeof(*NULL) y;\n"
"}", true); "}", true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("int * f() {\n"
" return NULL;\n"
"}\n"
"int main() {\n"
" return *f();\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Null pointer dereference: f()\n", errout.str());
} }
void gcc_statement_expression() { void gcc_statement_expression() {