Fixed #3861 (uninitialized variables)

This commit is contained in:
Daniel Marjamäki 2012-06-13 19:09:51 +02:00
parent 90f92250dd
commit cc5e06b5d2
2 changed files with 27 additions and 0 deletions

View File

@ -1275,6 +1275,15 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer) const
return true; return true;
} }
if (_tokenizer->isCPP() && Token::Match(vartok->next(), "<<|>>")) {
// Is variable a known POD type then this is a variable usage,
// otherwise we assume it's not.
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(vartok->varId());
if (var && var->typeStartToken()->isStandardType())
return true;
return false;
}
if (Token::Match(vartok->next(), "++|--|%op%")) if (Token::Match(vartok->next(), "++|--|%op%"))
return true; return true;

View File

@ -54,6 +54,7 @@ private:
TEST_CASE(uninitvar2); TEST_CASE(uninitvar2);
TEST_CASE(uninitvar3); // #3844 TEST_CASE(uninitvar3); // #3844
TEST_CASE(uninitvar4); // #3869 (reference) TEST_CASE(uninitvar4); // #3869 (reference)
TEST_CASE(uninitvar5); // #3861
} }
void checkUninitVar(const char code[]) { void checkUninitVar(const char code[]) {
@ -2161,6 +2162,23 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
// #3861
void uninitvar5() {
// ensure there is no false positive
checkUninitVar2("void f() {\n"
" x<char> c;\n"
" c << 2345;\n"
"}");
ASSERT_EQUALS("", errout.str());
// ensure there is no false negative
checkUninitVar2("void f() {\n"
" char c;\n"
" char a = c << 2;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n", errout.str());
}
}; };
REGISTER_TEST(TestUninitVar) REGISTER_TEST(TestUninitVar)