ericsesterhenn: Fixed #1091 (improve check: nullpointer dereference not detected)

This commit is contained in:
Daniel Marjamäki 2009-12-21 19:09:13 +01:00
parent 763e3d9eb5
commit b3b07321ca
2 changed files with 12 additions and 0 deletions

View File

@ -1198,6 +1198,8 @@ private:
dereference(foundError, checks, &tok); dereference(foundError, checks, &tok);
else if (Token::Match(tok.next(), ". %var%")) else if (Token::Match(tok.next(), ". %var%"))
dereference(foundError, checks, &tok); dereference(foundError, checks, &tok);
else if (Token::Match(tok.previous(), "[;{}] %var% [ %num% ] ="))
dereference(foundError, checks, &tok);
else else
bailOutVar(checks, tok.varId()); bailOutVar(checks, tok.varId());
} }

View File

@ -615,6 +615,8 @@ private:
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
CheckOther checkOther(&tokenizer, &settings, this); CheckOther checkOther(&tokenizer, &settings, this);
checkOther.nullPointer(); checkOther.nullPointer();
tokenizer.simplifyTokenList();
checkOther.executionPaths(); checkOther.executionPaths();
} }
@ -905,6 +907,14 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Possible null pointer dereference: p\n", errout.str()); ASSERT_EQUALS("[test.cpp:8]: (error) Possible null pointer dereference: p\n", errout.str());
checkNullPointer("static void foo()\n"
"{\n"
" int *p = 0;\n"
" int *q = p;\n"
" q[0] = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: q\n", errout.str());
// no false positive.. // no false positive..
checkNullPointer("static void foo()\n" checkNullPointer("static void foo()\n"
"{\n" "{\n"