CheckNullPointer::isPointerDeRef: Improve handling of static member variables and functions

This commit is contained in:
Dmitry-Me 2014-09-09 05:36:09 +02:00 committed by Daniel Marjamäki
parent e875146060
commit f7824bfd00
2 changed files with 34 additions and 0 deletions

View File

@ -168,6 +168,16 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown)
// read/write member variable // read/write member variable
if (firstOperand && parent->str() == "." && (!parent->astParent() || parent->astParent()->str() != "&")) { if (firstOperand && parent->str() == "." && (!parent->astParent() || parent->astParent()->str() != "&")) {
const Token* rightTok = parent->astOperand2();
if (rightTok) {
const Function* func = rightTok->function();
if (func && func->isStatic)
return false;
const Variable* var = rightTok->variable();
if (var && var->isStatic()) {
return false;
}
}
if (!parent->astParent() || parent->astParent()->str() != "(" || parent->astParent() == tok->previous()) if (!parent->astParent() || parent->astParent()->str() != "(" || parent->astParent() == tok->previous())
return true; return true;
unknown = true; unknown = true;

View File

@ -3173,6 +3173,30 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar2("class Element {\n"
" static int v;\n"
"};\n"
"void test() {\n"
" Element *element; element->v;\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("class Element {\n"
" void f() { }\n"
"};\n"
"void test() {\n"
" Element *element; element->f();\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
checkUninitVar2("class Element {\n"
" int v;\n"
"};\n"
"void test() {\n"
" Element *element; element->v;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
checkUninitVar2("void f() {\n" // #4911 - bad simplification => don't crash checkUninitVar2("void f() {\n" // #4911 - bad simplification => don't crash
" int a;\n" " int a;\n"
" do { a=do_something() } while (a);\n" " do { a=do_something() } while (a);\n"