Fixed #6871 (False positive uninitvar - pointer to uninitialized char[])

This commit is contained in:
Daniel Marjamäki 2015-08-08 14:52:46 +02:00
parent 72706cd0b8
commit e71ab70356
2 changed files with 31 additions and 0 deletions

View File

@ -218,6 +218,17 @@ static bool isVariableUsed(const Token *tok, const Variable& var)
return false;
if (tok->isConstOp())
return isVariableUsed(tok->astOperand1(),var) || isVariableUsed(tok->astOperand2(),var);
if (var.isArray()) {
const Token *parent = tok->astParent();
while (Token::Match(parent, "[?:]"))
parent = parent->astParent();
// no dereference, then array is not "used"
if (!Token::Match(parent, "*|["))
return false;
const Token *parent2 = parent->astParent();
// TODO: handle function calls. There is a TODO assertion in TestUninitVar::uninitvar_arrays
return !parent2 || parent2->isConstOp() || (parent2->str() == "=" && parent2->astOperand2() == parent);
}
return (tok->varId() == var.declarationId());
}

View File

@ -1254,6 +1254,26 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
// array usage in ?: (tests that the isVariableUsed() works)
checkUninitVar("void f() {\n"
" char a[10], *p;\n"
" p = c?a:0;\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n"
" char a[10], c;\n"
" c = *(x?a:0);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void f() {\n"
" char a[10], c;\n"
" strcpy(dest, x?a:\"\");\n"
"}");
TODO_ASSERT_EQUALS("error", "", errout.str());
// passing array to library functions
checkUninitVar("void f()\n"
"{\n"
" char c[50] = \"\";\n"