Uninitialized variables; Fixed false positive for overloaded & 'ar & a & b & c'

This commit is contained in:
Daniel Marjamäki 2021-05-18 21:03:43 +02:00
parent 216918756b
commit e35c46bcb9
2 changed files with 20 additions and 2 deletions

View File

@ -1166,8 +1166,19 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer,
if (astIsRhs(derefValue) && isLikelyStreamRead(mTokenizer->isCPP(), derefValue->astParent())) if (astIsRhs(derefValue) && isLikelyStreamRead(mTokenizer->isCPP(), derefValue->astParent()))
return nullptr; return nullptr;
if (mTokenizer->isCPP() && Token::simpleMatch(valueExpr->astParent(), "&") && !valueExpr->astParent()->astParent() && astIsRhs(valueExpr) && Token::Match(valueExpr->astSibling(), "%type%")) // Assignment with overloaded &
return nullptr; if (mTokenizer->isCPP() && Token::simpleMatch(valueExpr->astParent(), "&") && astIsRhs(valueExpr)) {
const Token *parent = valueExpr->astParent();
while (Token::simpleMatch(parent, "&") && parent->isBinaryOp())
parent = parent->astParent();
if (!parent) {
const Token *lhs = valueExpr->astParent();
while (Token::simpleMatch(lhs, "&") && lhs->isBinaryOp())
lhs = lhs->astOperand1();
if (lhs && lhs->isName() && (!lhs->valueType() || lhs->valueType()->type <= ValueType::Type::CONTAINER))
return nullptr; // <- possible assignment
}
}
return derefValue ? derefValue : valueExpr; return derefValue ? derefValue : valueExpr;
} }

View File

@ -534,12 +534,19 @@ private:
" a & x;\n" " a & x;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f(int a) {\n" checkUninitVar("void f(int a) {\n"
" int x;\n" " int x;\n"
" a & x;\n" " a & x;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
checkUninitVar("void f() {\n"
" int a,b,c;\n"
" ar & a & b & c;\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void a() {\n" // asm checkUninitVar("void a() {\n" // asm
" int x;\n" " int x;\n"
" asm();\n" " asm();\n"