Skip to content

Commit 5d2a39b

Browse files
committed
CheckNullPointer: improved function call checking when new value flow analysis is used
1 parent 2e67ca0 commit 5d2a39b

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

lib/checknullpointer.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -762,18 +762,42 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
762762
if (!var || !var->isPointer())
763763
continue;
764764

765+
const ValueFlow::Value *value = 0;
766+
for (std::list<ValueFlow::Value>::const_iterator it = tok->values.begin(); it != tok->values.end(); ++it) {
767+
if (it->intvalue == 0) {
768+
value = &(*it);
769+
break;
770+
}
771+
}
772+
if (!value)
773+
continue;
774+
775+
if (Token::Match(tok->previous(), "[(,] %var% [,)]")) {
776+
const Token *ftok = tok->previous();
777+
while (ftok && ftok->str() != "(") {
778+
if (ftok->str() == ")")
779+
ftok = ftok->link();
780+
ftok = ftok->previous();
781+
}
782+
std::list<const Token *> varlist;
783+
parseFunctionCall(*ftok->previous(), varlist, &_settings->library, 0);
784+
if (std::find(varlist.begin(), varlist.end(), tok) != varlist.end()) {
785+
if (value->condition == NULL)
786+
nullPointerError(tok);
787+
else if (_settings->isEnabled("warning"))
788+
nullPointerError(tok, tok->str(), value->condition, false);
789+
}
790+
continue;
791+
}
792+
765793
bool unknown = false;
766794
if (!isPointerDeRef(tok,unknown))
767795
continue;
768796

769-
for (std::list<ValueFlow::Value>::const_iterator it = tok->values.begin(); it != tok->values.end(); ++it) {
770-
if (it->intvalue != 0)
771-
continue;
772-
if (it->condition == NULL)
773-
nullPointerError(tok);
774-
else if (_settings->isEnabled("warning"))
775-
nullPointerError(tok, tok->str(), it->condition, false);
776-
}
797+
if (value->condition == NULL)
798+
nullPointerError(tok);
799+
else if (_settings->isEnabled("warning"))
800+
nullPointerError(tok, tok->str(), value->condition, false);
777801
}
778802
return;
779803
}

test/testnullpointer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ class TestNullPointer : public TestFixture {
8888
settings.inconclusive = inconclusive;
8989
//settings.valueFlow = true;
9090

91+
// cfg
92+
const char cfg[] = "<?xml version=\"1.0\"?>\n"
93+
"<def>\n"
94+
" <function name=\"strcpy\"> <arg nr=\"1\"><not-null/></arg> </function>\n"
95+
"</def>";
96+
settings.library.loadxmldata(cfg, sizeof(cfg));
97+
9198
// Tokenize..
9299
Tokenizer tokenizer(&settings, this);
93100
std::istringstream istr(code);

0 commit comments

Comments
 (0)