Fix FP uninitStructMember when other variable is only nonzero if struct member is initialized (#5143)

This commit is contained in:
Daniel Marjamäki 2023-06-10 21:35:17 +02:00 committed by GitHub
parent 7840bc4c63
commit f6a65afdee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View File

@ -418,9 +418,15 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
continue;
}
// assignment with nonzero constant..
if (Token::Match(tok->previous(), "[;{}] %var% = - %name% ;"))
variableValue[tok->varId()] = !VariableValue(0);
// track values of other variables..
if (Token::Match(tok->previous(), "[;{}] %var% =")) {
if (tok->next()->astOperand2() && tok->next()->astOperand2()->hasKnownIntValue())
variableValue[tok->varId()] = VariableValue(tok->next()->astOperand2()->getKnownIntValue());
else if (Token::Match(tok->previous(), "[;{}] %var% = - %name% ;"))
variableValue[tok->varId()] = !VariableValue(0);
else
variableValue.erase(tok->varId());
}
// Inner scope..
else if (Token::simpleMatch(tok, "if (")) {

View File

@ -4743,6 +4743,20 @@ private:
" char* q = (s).p;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// if with flag
checkUninitVar("struct AB { int a; int b; };\n"
"int f(int x) {\n"
" struct AB ab;\n"
" int flag = 0;\n"
" if (x == 0) {\n"
" flag = dostuff(&ab);\n"
" }\n"
" if (flag) {\n"
" a = ab.a;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void uninitvar2_while() {