Fixed #6871 (False positive uninitvar - pointer to uninitialized char[])
This commit is contained in:
parent
72706cd0b8
commit
e71ab70356
|
@ -218,6 +218,17 @@ static bool isVariableUsed(const Token *tok, const Variable& var)
|
||||||
return false;
|
return false;
|
||||||
if (tok->isConstOp())
|
if (tok->isConstOp())
|
||||||
return isVariableUsed(tok->astOperand1(),var) || isVariableUsed(tok->astOperand2(),var);
|
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());
|
return (tok->varId() == var.declarationId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1254,6 +1254,26 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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"
|
checkUninitVar("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" char c[50] = \"\";\n"
|
" char c[50] = \"\";\n"
|
||||||
|
|
Loading…
Reference in New Issue