CheckNullPointer: improved function call checking when new value flow analysis is used

This commit is contained in:
Daniel Marjamäki 2014-01-12 19:27:26 +01:00
parent 2e67ca06c0
commit 5d2a39b580
2 changed files with 39 additions and 8 deletions

View File

@ -762,18 +762,42 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
if (!var || !var->isPointer())
continue;
const ValueFlow::Value *value = 0;
for (std::list<ValueFlow::Value>::const_iterator it = tok->values.begin(); it != tok->values.end(); ++it) {
if (it->intvalue == 0) {
value = &(*it);
break;
}
}
if (!value)
continue;
if (Token::Match(tok->previous(), "[(,] %var% [,)]")) {
const Token *ftok = tok->previous();
while (ftok && ftok->str() != "(") {
if (ftok->str() == ")")
ftok = ftok->link();
ftok = ftok->previous();
}
std::list<const Token *> varlist;
parseFunctionCall(*ftok->previous(), varlist, &_settings->library, 0);
if (std::find(varlist.begin(), varlist.end(), tok) != varlist.end()) {
if (value->condition == NULL)
nullPointerError(tok);
else if (_settings->isEnabled("warning"))
nullPointerError(tok, tok->str(), value->condition, false);
}
continue;
}
bool unknown = false;
if (!isPointerDeRef(tok,unknown))
continue;
for (std::list<ValueFlow::Value>::const_iterator it = tok->values.begin(); it != tok->values.end(); ++it) {
if (it->intvalue != 0)
continue;
if (it->condition == NULL)
nullPointerError(tok);
else if (_settings->isEnabled("warning"))
nullPointerError(tok, tok->str(), it->condition, false);
}
if (value->condition == NULL)
nullPointerError(tok);
else if (_settings->isEnabled("warning"))
nullPointerError(tok, tok->str(), value->condition, false);
}
return;
}

View File

@ -88,6 +88,13 @@ private:
settings.inconclusive = inconclusive;
//settings.valueFlow = true;
// cfg
const char cfg[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"strcpy\"> <arg nr=\"1\"><not-null/></arg> </function>\n"
"</def>";
settings.library.loadxmldata(cfg, sizeof(cfg));
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);