Uninitialized variables; Fixed false positives for reference cast and dereferencing address of uninitialized variable

This commit is contained in:
Daniel Marjamäki 2021-05-24 17:50:28 +02:00
parent 4ad90bf6f1
commit b7803ea6fb
2 changed files with 26 additions and 0 deletions

View File

@ -1098,6 +1098,17 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer,
const Token *valueExpr = vartok; // non-dereferenced , no address of value as variable
while (Token::Match(valueExpr->astParent(), ".|::") && astIsRhs(valueExpr))
valueExpr = valueExpr->astParent();
// stuff we ignore..
while (valueExpr->astParent()) {
// *&x
if (valueExpr->astParent()->isUnaryOp("&") && valueExpr->astParent()->astParent() && valueExpr->astParent()->astParent()->isUnaryOp("*"))
valueExpr = valueExpr->astParent()->astParent();
// (type &)x
else if (valueExpr->astParent()->isCast() && valueExpr->astParent()->isUnaryOp("(") && Token::simpleMatch(valueExpr->astParent()->link()->previous(), "& )"))
valueExpr = valueExpr->astParent();
else
break;
}
if (!pointer) {
if (Token::Match(vartok, "%name% [.(]") && vartok->variable() && !vartok->variable()->isPointer())
return nullptr;

View File

@ -57,6 +57,7 @@ private:
TEST_CASE(func_uninit_var); // analyse function calls for: 'int a(int x) { return x+x; }'
TEST_CASE(func_uninit_pointer); // analyse function calls for: 'void a(int *p) { *p = 0; }'
TEST_CASE(uninitvar_typeof); // typeof
TEST_CASE(uninitvar_ignore); // ignore cast, *&x, ..
TEST_CASE(uninitvar2);
TEST_CASE(uninitvar3); // #3844
TEST_CASE(uninitvar4); // #3869 (reference)
@ -2325,6 +2326,20 @@ private:
ASSERT_EQUALS("", errout.str());
}
void uninitvar_ignore() {
checkUninitVar("void foo() {\n"
" int i;\n"
" dostuff((int&)i, 0);\n" // <- cast is not use
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo() {\n"
" int i;\n"
" dostuff(*&i, 0);\n" // <- *& is not use
"}");
ASSERT_EQUALS("", errout.str());
}
void uninitvar2() {
// using uninit var
checkUninitVar("void f() {\n"