Skip to content

Commit c4fd891

Browse files
committed
Fixed danmar#6014: Added plausibility check before issuing null pointer messages on function calls defined in library
1 parent 88990ba commit c4fd891

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/checknullpointer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ namespace {
3131

3232
//---------------------------------------------------------------------------
3333

34+
static bool checkNullpointerFunctionCallPlausibility(const Function* func, unsigned int arg)
35+
{
36+
return !func || (func->argCount() >= arg && func->getArgumentVar(arg - 1) && func->getArgumentVar(arg - 1)->isPointer());
37+
}
38+
3439
/**
3540
* @brief parse a function call and extract information about variable usage
3641
* @param tok first token
@@ -52,17 +57,17 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
5257
(value == 0 && Token::Match(firstParam, "0|NULL ,|)"))) {
5358
if (value == 0 && Token::Match(&tok, "snprintf|vsnprintf|fnprintf|vfnprintf") && secondParam && secondParam->str() != "0") // Only if length (second parameter) is not zero
5459
var.push_back(firstParam);
55-
else if (value == 0 && library != nullptr && library->isnullargbad(tok.str(),1))
60+
else if (value == 0 && library != nullptr && library->isnullargbad(tok.str(), 1) && checkNullpointerFunctionCallPlausibility(tok.function(), 1))
5661
var.push_back(firstParam);
57-
else if (value == 1 && library != nullptr && library->isuninitargbad(tok.str(),1))
62+
else if (value == 1 && library != nullptr && library->isuninitargbad(tok.str(), 1))
5863
var.push_back(firstParam);
5964
}
6065

6166
// 2nd parameter..
6267
if ((value == 0 && Token::Match(secondParam, "0|NULL ,|)")) || (secondParam && secondParam->varId() > 0 && Token::Match(secondParam->next(),"[,)]"))) {
63-
if (value == 0 && library != nullptr && library->isnullargbad(tok.str(),2))
68+
if (value == 0 && library != nullptr && library->isnullargbad(tok.str(), 2) && checkNullpointerFunctionCallPlausibility(tok.function(), 2))
6469
var.push_back(secondParam);
65-
else if (value == 1 && library != nullptr && library->isuninitargbad(tok.str(),2))
70+
else if (value == 1 && library != nullptr && library->isuninitargbad(tok.str(), 2))
6671
var.push_back(secondParam);
6772
}
6873

test/testnullpointer.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class TestNullPointer : public TestFixture {
5959
TEST_CASE(nullpointer24); // #5082 fp: chained assignment
6060
TEST_CASE(nullpointer25); // #5061
6161
TEST_CASE(nullpointer26); // #3589
62+
TEST_CASE(nullpointer27); // #6014
6263
TEST_CASE(nullpointerSwitch); // #2626
6364
TEST_CASE(nullpointer_cast); // #4692
6465
TEST_CASE(nullpointer_castToVoid); // #3771
@@ -1304,6 +1305,36 @@ class TestNullPointer : public TestFixture {
13041305
ASSERT_EQUALS("", errout.str());
13051306
}
13061307

1308+
void nullpointer27() { // #6014
1309+
check("void fgetpos(int x, int y);\n"
1310+
"void foo() {\n"
1311+
" fgetpos(0, x);\n"
1312+
" fgetpos(x, 0);\n"
1313+
"}");
1314+
ASSERT_EQUALS("", errout.str());
1315+
1316+
check("void fgetpos(void* x, int y);\n"
1317+
"void foo() {\n"
1318+
" fgetpos(0, x);\n"
1319+
" fgetpos(x, 0);\n"
1320+
"}");
1321+
ASSERT_EQUALS("[test.cpp:3]: (error) Null pointer dereference\n", errout.str());
1322+
1323+
check("void fgetpos(int x, void* y);\n"
1324+
"void foo() {\n"
1325+
" fgetpos(0, x);\n"
1326+
" fgetpos(x, 0);\n"
1327+
"}");
1328+
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str());
1329+
1330+
check("void foo() {\n"
1331+
" fgetpos(0, x);\n"
1332+
" fgetpos(x, 0);\n"
1333+
"}");
1334+
ASSERT_EQUALS("[test.cpp:2]: (error) Null pointer dereference\n"
1335+
"[test.cpp:3]: (error) Null pointer dereference\n", errout.str());
1336+
}
1337+
13071338
void nullpointerSwitch() { // #2626
13081339
check("char *f(int x) {\n"
13091340
" char *p = do_something();\n"

0 commit comments

Comments
 (0)