Uninitialized variables: Fixed false negatives for loop variables / pointer dereference
This commit is contained in:
parent
596b4bd405
commit
e2fa6a291c
|
@ -1068,18 +1068,6 @@ void CheckUninitVar::checkScope(const Scope* scope)
|
|||
continue;
|
||||
if (i->nameToken()->strAt(1) == "(")
|
||||
continue;
|
||||
|
||||
bool forHead = false; // Don't check variables declared in header of a for loop
|
||||
for (const Token* tok = i->typeStartToken(); tok; tok = tok->previous()) {
|
||||
if (tok->str() == "(") {
|
||||
forHead = true;
|
||||
break;
|
||||
} else if (Token::Match(tok, "[{};]"))
|
||||
break;
|
||||
}
|
||||
if (forHead)
|
||||
continue;
|
||||
|
||||
bool stdtype = _tokenizer->isC();
|
||||
const Token* tok = i->typeStartToken();
|
||||
for (; tok && tok->str() != ";" && tok->str() != "<"; tok = tok->next()) {
|
||||
|
@ -1090,8 +1078,10 @@ void CheckUninitVar::checkScope(const Scope* scope)
|
|||
tok = tok->next();
|
||||
if (!tok)
|
||||
continue;
|
||||
if (Token::findsimplematch(i->typeStartToken(), "=", tok))
|
||||
if (Token::Match(i->nameToken(), "%var% =")) {
|
||||
checkRhs(i->nameToken(), *i, false, "");
|
||||
continue;
|
||||
}
|
||||
if (stdtype || i->isPointer()) {
|
||||
bool alloc = false;
|
||||
checkScopeForVariable(scope, tok, *i, NULL, NULL, &alloc, "");
|
||||
|
@ -1780,7 +1770,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
|
|||
}
|
||||
|
||||
bool unknown = false;
|
||||
if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
|
||||
if (pointer && (CheckNullPointer::isPointerDeRef(vartok, unknown) || Token::Match(vartok, "%var% ."))) {
|
||||
// pointer is allocated - dereferencing it is ok.
|
||||
if (alloc)
|
||||
return false;
|
||||
|
|
|
@ -225,7 +225,7 @@ bool Library::load(const tinyxml2::XMLDocument &doc)
|
|||
}
|
||||
|
||||
else if (strcmp(markupnode->Name(), "codeblocks") == 0) {
|
||||
for (const tinyxml2::XMLElement *blocknode = blocknode->FirstChildElement(); blocknode; blocknode = blocknode->NextSiblingElement()) {
|
||||
for (const tinyxml2::XMLElement *blocknode = markupnode->FirstChildElement(); blocknode; blocknode = blocknode->NextSiblingElement()) {
|
||||
if (strcmp(blocknode->Name(), "block") == 0)
|
||||
_executableblocks[extension].addBlock(blocknode->Attribute("name"));
|
||||
|
||||
|
|
|
@ -3065,6 +3065,16 @@ private:
|
|||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
||||
|
||||
checkUninitVar2("void f() {\n"
|
||||
" for (int x = x; x < 10; x++) {}\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: x\n", errout.str());
|
||||
|
||||
checkUninitVar2("void f() {\n"
|
||||
" for (Element *ptr3 = ptr3->Next(); ptr3; ptr3 = ptr3->Next()) {}\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: ptr3\n", errout.str());
|
||||
|
||||
checkUninitVar2("void f() {\n" // #4911 - bad simplification => don't crash
|
||||
" int a;\n"
|
||||
" do { } a=do_something(); while (a);\n"
|
||||
|
|
Loading…
Reference in New Issue