Skip to content

Commit

Permalink
Partial fix #12030 (False positive: uninitialized variable, condition…
Browse files Browse the repository at this point in the history
…al modification, flag) (#5543)
  • Loading branch information
danmar authored Oct 12, 2023
1 parent 8c0d43d commit 9d4e382
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
63 changes: 36 additions & 27 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ static const Token *getAstParentSkipPossibleCastAndAddressOf(const Token *vartok
return parent;
}

static std::map<nonneg int, VariableValue> getVariableValues(const Token* tok) {
std::map<nonneg int, VariableValue> ret;
if (!tok || !tok->scope()->isExecutable())
return ret;
while (tok && tok->str() != "{") {
if (tok->str() == "}") {
if (tok->link()->isBinaryOp())
tok = tok->link()->previous();
else
break;
}
if (Token::Match(tok, "%var% =|{") && tok->next()->isBinaryOp() && tok->varId() && ret.count(tok->varId()) == 0) {
const Token* rhs = tok->next()->astOperand2();
if (rhs && rhs->hasKnownIntValue())
ret[tok->varId()] = VariableValue(rhs->getKnownIntValue());
}
tok = tok->previous();
}
return ret;
}

bool CheckUninitVar::diag(const Token* tok)
{
if (!tok)
Expand Down Expand Up @@ -175,14 +196,14 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string>
}
if (!init) {
Alloc alloc = ARRAY;
const std::map<nonneg int, VariableValue> variableValue;
std::map<nonneg int, VariableValue> variableValue = getVariableValues(var.typeStartToken());
checkScopeForVariable(tok, var, nullptr, nullptr, &alloc, emptyString, variableValue);
}
continue;
}
if (stdtype || var.isPointer()) {
Alloc alloc = NO_ALLOC;
const std::map<nonneg int, VariableValue> variableValue;
std::map<nonneg int, VariableValue> variableValue = getVariableValues(var.typeStartToken());
checkScopeForVariable(tok, var, nullptr, nullptr, &alloc, emptyString, variableValue);
}
if (var.type())
Expand All @@ -207,7 +228,7 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string>
checkStruct(tok, arg);
else if (arg.typeStartToken()->isStandardType() || arg.typeStartToken()->isEnumType()) {
Alloc alloc = NO_ALLOC;
const std::map<nonneg int, VariableValue> variableValue;
std::map<nonneg int, VariableValue> variableValue;
checkScopeForVariable(tok->next(), arg, nullptr, nullptr, &alloc, emptyString, variableValue);
}
}
Expand Down Expand Up @@ -246,7 +267,7 @@ void CheckUninitVar::checkStruct(const Token *tok, const Variable &structvar)
const Token *tok2 = tok;
if (tok->str() == "}")
tok2 = tok2->next();
const std::map<nonneg int, VariableValue> variableValue;
std::map<nonneg int, VariableValue> variableValue = getVariableValues(structvar.typeStartToken());
checkScopeForVariable(tok2, structvar, nullptr, nullptr, &alloc, var.name(), variableValue);
}
}
Expand Down Expand Up @@ -377,7 +398,7 @@ static bool isVariableUsed(const Token *tok, const Variable& var)
return !parent2 || parent2->isConstOp() || (parent2->str() == "=" && parent2->astOperand2() == parent);
}

bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var, bool * const possibleInit, bool * const noreturn, Alloc* const alloc, const std::string &membervar, std::map<nonneg int, VariableValue> variableValue)
bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var, bool * const possibleInit, bool * const noreturn, Alloc* const alloc, const std::string &membervar, std::map<nonneg int, VariableValue>& variableValue)
{
const bool suppressErrors(possibleInit && *possibleInit); // Assume that this is a variable declaration, rather than a fundef
const bool printDebug = mSettings->debugwarnings;
Expand Down Expand Up @@ -421,7 +442,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
}

// track values of other variables..
if (Token::Match(tok->previous(), "[;{}] %var% =")) {
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% ;"))
Expand Down Expand Up @@ -489,7 +510,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
if (tok->str() == "{") {
bool possibleInitIf((!alwaysTrue && number_of_if > 0) || suppressErrors);
bool noreturnIf = false;
const bool initif = !alwaysFalse && checkScopeForVariable(tok->next(), var, &possibleInitIf, &noreturnIf, alloc, membervar, variableValue);
std::map<nonneg int, VariableValue> varValueIf(variableValue);
const bool initif = !alwaysFalse && checkScopeForVariable(tok->next(), var, &possibleInitIf, &noreturnIf, alloc, membervar, varValueIf);

// bail out for such code:
// if (a) x=0; // conditional initialization
Expand All @@ -511,15 +533,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
if (alwaysTrue && (initif || noreturnIf))
return true;

std::map<nonneg int, VariableValue> varValueIf;
if (!alwaysFalse && !initif && !noreturnIf) {
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
if (Token::Match(tok2, "[;{}.] %name% = - %name% ;"))
varValueIf[tok2->next()->varId()] = !VariableValue(0);
else if (Token::Match(tok2, "[;{}.] %name% = %num% ;"))
varValueIf[tok2->next()->varId()] = VariableValue(MathLib::toBigNumber(tok2->strAt(3)));
}
}
if (!alwaysFalse && !initif && !noreturnIf)
variableValue = varValueIf;

if (initif && condVarId > 0)
variableValue[condVarId] = !condVarValue;
Expand All @@ -539,17 +554,11 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var

bool possibleInitElse((!alwaysFalse && number_of_if > 0) || suppressErrors);
bool noreturnElse = false;
const bool initelse = !alwaysTrue && checkScopeForVariable(tok->next(), var, &possibleInitElse, &noreturnElse, alloc, membervar, variableValue);

std::map<nonneg int, VariableValue> varValueElse;
if (!alwaysTrue && !initelse && !noreturnElse) {
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
if (Token::Match(tok2, "[;{}.] %var% = - %name% ;"))
varValueElse[tok2->next()->varId()] = !VariableValue(0);
else if (Token::Match(tok2, "[;{}.] %var% = %num% ;"))
varValueElse[tok2->next()->varId()] = VariableValue(MathLib::toBigNumber(tok2->strAt(3)));
}
}
std::map<nonneg int, VariableValue> varValueElse(variableValue);
const bool initelse = !alwaysTrue && checkScopeForVariable(tok->next(), var, &possibleInitElse, &noreturnElse, alloc, membervar, varValueElse);

if (!alwaysTrue && !initelse && !noreturnElse)
variableValue = varValueElse;

if (initelse && condVarId > 0 && !noreturnIf && !noreturnElse)
variableValue[condVarId] = condVarValue;
Expand Down
2 changes: 1 addition & 1 deletion lib/checkuninitvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class CPPCHECKLIB CheckUninitVar : public Check {
void check();
void checkScope(const Scope* scope, const std::set<std::string> &arrayTypeDefs);
void checkStruct(const Token *tok, const Variable &structvar);
bool checkScopeForVariable(const Token *tok, const Variable& var, bool* const possibleInit, bool* const noreturn, Alloc* const alloc, const std::string &membervar, std::map<nonneg int, VariableValue> variableValue);
bool checkScopeForVariable(const Token *tok, const Variable& var, bool* const possibleInit, bool* const noreturn, Alloc* const alloc, const std::string &membervar, std::map<nonneg int, VariableValue>& variableValue);
const Token* checkExpr(const Token* tok, const Variable& var, const Alloc alloc, bool known, bool* bailout = nullptr) const;
bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, Alloc alloc, const std::string &membervar);
bool checkLoopBody(const Token *tok, const Variable& var, const Alloc alloc, const std::string &membervar, const bool suppressErrors);
Expand Down
12 changes: 12 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,18 @@ class TestUninitVar : public TestFixture {
" return v2 + v4 + v6 + v7;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:12]: (error) Uninitialized variable: v5\n", errout.str());

checkUninitVar("bool set(int *p);\n"
"\n"
"void foo(bool a) {\n"
" bool flag{false};\n"
" int x;\n"
" if (!a) {\n"
" flag = set(&x);\n"
" }\n"
" if (!flag || x == 0) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}


Expand Down

0 comments on commit 9d4e382

Please sign in to comment.