Fix #11832 False positive: uninitialized variable '*(&var) = 0' (#5241)

This commit is contained in:
chrchr-github 2023-07-14 19:14:33 +02:00 committed by GitHub
parent a4a29bfbc5
commit 72212331fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 2 deletions

View File

@ -1248,7 +1248,7 @@ const Token* CheckUninitVar::isVariableUsage(bool cpp, const Token *vartok, cons
}
if (Token::simpleMatch(tok->astParent(), "=")) {
if (astIsLhs(tok)) {
if (alloc == ARRAY || !derefValue || !derefValue->isUnaryOp("*"))
if (alloc == ARRAY || !derefValue || !derefValue->isUnaryOp("*") || !pointer)
return nullptr;
const Token* deref = derefValue->astOperand1();
while (deref && deref->isCast())

View File

@ -1009,7 +1009,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
// assignment
else if ((Token::Match(tok, "%name% [") && Token::simpleMatch(skipBracketsAndMembers(tok->next()), "=")) ||
(Token::simpleMatch(tok, "* (") && Token::simpleMatch(tok->next()->link(), ") ="))) {
(tok->isUnaryOp("*") && Token::simpleMatch(tok->astParent(), "=") && Token::simpleMatch(tok->astOperand1(), "+"))) {
const Token *eq = tok;
while (eq && !eq->isAssignmentOp())
eq = eq->astParent();

View File

@ -75,6 +75,7 @@ private:
TEST_CASE(uninitvar11); // ticket #9123
TEST_CASE(uninitvar12); // #10218 - stream read
TEST_CASE(uninitvar13); // #9772
TEST_CASE(uninitvar14);
TEST_CASE(uninitvar_unconditionalTry);
TEST_CASE(uninitvar_funcptr); // #6404
TEST_CASE(uninitvar_operator); // #6680
@ -3126,6 +3127,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void uninitvar14() { // #11832
const char code[] = "void f() {\n"
" int b;\n"
" *(&b) = 0;\n"
"}";
checkUninitVar(code);
ASSERT_EQUALS("", errout.str());
}
void uninitvar_unconditionalTry() {
// Unconditional scopes and try{} scopes
checkUninitVar("int f() {\n"

View File

@ -2640,6 +2640,12 @@ private:
" *(b+i) = 0;\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Variable '*(b+i)' is assigned a value that is never used.\n", "", errout.str());
functionVariableUsage("void f() {\n" // #11832
" int b;\n"
" *(&b) = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvar8() {