value flow: starting to refactor CheckNullPointer::nullPointerDeRefThenCheck
This commit is contained in:
parent
26a72d73fe
commit
7c4a7ac3d5
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -86,6 +86,7 @@ private:
|
|||
Settings settings;
|
||||
settings.addEnabled("warning");
|
||||
settings.inconclusive = inconclusive;
|
||||
//settings.valueFlow = true;
|
||||
|
||||
// Tokenize..
|
||||
Tokenizer tokenizer(&settings, this);
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue