value flow: starting to refactor CheckNullPointer::nullPointerDeRefThenCheck

This commit is contained in:
Daniel Marjamäki 2014-01-08 17:37:39 +01:00
parent 26a72d73fe
commit 7c4a7ac3d5
4 changed files with 33 additions and 1 deletions

View File

@ -753,6 +753,31 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
{
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
if (_settings->valueFlow) {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (!tok->isName() || !tok->values.empty())
continue;
const Variable *var = tok->variable();
if (!var || !var->isPointer())
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);
}
}
return;
}
// Dereferencing a pointer and then checking if it's NULL..
// This check will first scan for the check. And then scan backwards
// from the check, searching for dereferencing.

View File

@ -103,7 +103,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
if (tok2->varId() == varid) {
// bailout: assignment
if (Token::Match(tok2, "%var% =")) {
if (Token::Match(tok2->previous(), "!!* %var% =")) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "assignment of " + tok2->str());
break;

View File

@ -86,6 +86,7 @@ private:
Settings settings;
settings.addEnabled("warning");
settings.inconclusive = inconclusive;
//settings.valueFlow = true;
// Tokenize..
Tokenizer tokenizer(&settings, this);

View File

@ -91,6 +91,12 @@ private:
ASSERT_EQUALS(true, testValueOfX(code, 2U, 1));
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
code = "void f(int *x) {\n"
" *x = 100;\n"
" if (x) {}\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
// bailout: ?:
bailout("void f(int x) {\n"
" y = ((x<0) ? x : ((x==2)?3:4));\n"