Fixed #4934 (false positive: Uninitialized variable (loop with inner condition '(x=1)'))

This commit is contained in:
Daniel Marjamäki 2013-09-23 06:07:01 +02:00
parent 1feabd0f0a
commit 64454068ec
2 changed files with 19 additions and 1 deletions

View File

@ -1518,10 +1518,17 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
else {
bool assign = true;
if (tok->strAt(1) == "=") {
unsigned int indentlevel = 0; // Handle '(a=1)..'
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->varId() == var.declarationId()) {
assign = false;
break;
} else if (tok2->str() == "(") {
++indentlevel;
} else if (tok2->str() == ")") {
if (indentlevel <= 1U)
break;
--indentlevel;
}
}
}

View File

@ -2950,9 +2950,20 @@ private:
" }\n"
" }\n"
" return x;\n"
"}\n", "test.c");
"}\n");
TODO_ASSERT_EQUALS("error", "", errout.str());
checkUninitVar2("int f(void) {\n"
" int x;\n"
" while (a()) {\n"
" if (b() && (x=1)) {\n"
" return x;\n"
" }\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("void f(void) {\n"
" int x;\n"
" for (;;) {\n"