Skip to content

Commit e2fa6a2

Browse files
committed
Uninitialized variables: Fixed false negatives for loop variables / pointer dereference
1 parent 596b4bd commit e2fa6a2

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,18 +1068,6 @@ void CheckUninitVar::checkScope(const Scope* scope)
10681068
continue;
10691069
if (i->nameToken()->strAt(1) == "(")
10701070
continue;
1071-
1072-
bool forHead = false; // Don't check variables declared in header of a for loop
1073-
for (const Token* tok = i->typeStartToken(); tok; tok = tok->previous()) {
1074-
if (tok->str() == "(") {
1075-
forHead = true;
1076-
break;
1077-
} else if (Token::Match(tok, "[{};]"))
1078-
break;
1079-
}
1080-
if (forHead)
1081-
continue;
1082-
10831071
bool stdtype = _tokenizer->isC();
10841072
const Token* tok = i->typeStartToken();
10851073
for (; tok && tok->str() != ";" && tok->str() != "<"; tok = tok->next()) {
@@ -1090,8 +1078,10 @@ void CheckUninitVar::checkScope(const Scope* scope)
10901078
tok = tok->next();
10911079
if (!tok)
10921080
continue;
1093-
if (Token::findsimplematch(i->typeStartToken(), "=", tok))
1081+
if (Token::Match(i->nameToken(), "%var% =")) {
1082+
checkRhs(i->nameToken(), *i, false, "");
10941083
continue;
1084+
}
10951085
if (stdtype || i->isPointer()) {
10961086
bool alloc = false;
10971087
checkScopeForVariable(scope, tok, *i, NULL, NULL, &alloc, "");
@@ -1780,7 +1770,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
17801770
}
17811771

17821772
bool unknown = false;
1783-
if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
1773+
if (pointer && (CheckNullPointer::isPointerDeRef(vartok, unknown) || Token::Match(vartok, "%var% ."))) {
17841774
// pointer is allocated - dereferencing it is ok.
17851775
if (alloc)
17861776
return false;

lib/library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ bool Library::load(const tinyxml2::XMLDocument &doc)
225225
}
226226

227227
else if (strcmp(markupnode->Name(), "codeblocks") == 0) {
228-
for (const tinyxml2::XMLElement *blocknode = blocknode->FirstChildElement(); blocknode; blocknode = blocknode->NextSiblingElement()) {
228+
for (const tinyxml2::XMLElement *blocknode = markupnode->FirstChildElement(); blocknode; blocknode = blocknode->NextSiblingElement()) {
229229
if (strcmp(blocknode->Name(), "block") == 0)
230230
_executableblocks[extension].addBlock(blocknode->Attribute("name"));
231231

test/testuninitvar.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,6 +3065,16 @@ class TestUninitVar : public TestFixture {
30653065
"}");
30663066
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
30673067

3068+
checkUninitVar2("void f() {\n"
3069+
" for (int x = x; x < 10; x++) {}\n"
3070+
"}");
3071+
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: x\n", errout.str());
3072+
3073+
checkUninitVar2("void f() {\n"
3074+
" for (Element *ptr3 = ptr3->Next(); ptr3; ptr3 = ptr3->Next()) {}\n"
3075+
"}");
3076+
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: ptr3\n", errout.str());
3077+
30683078
checkUninitVar2("void f() {\n" // #4911 - bad simplification => don't crash
30693079
" int a;\n"
30703080
" do { } a=do_something(); while (a);\n"

0 commit comments

Comments
 (0)