diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index bd96f61b6..a1871fb0f 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -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::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. diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 0117a8b21..181529270 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 67483f823..ab7e30032 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -86,6 +86,7 @@ private: Settings settings; settings.addEnabled("warning"); settings.inconclusive = inconclusive; + //settings.valueFlow = true; // Tokenize.. Tokenizer tokenizer(&settings, this); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index fa421625d..4ab1bee8e 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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"