Skip to content

Commit

Permalink
fixed #13513 - corrected access of known int values [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Jan 5, 2025
1 parent 9871a09 commit 516a7f3
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 52 deletions.
8 changes: 4 additions & 4 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ static bool match(const Token *tok, const std::string &rhs)
{
if (tok->str() == rhs)
return true;
if (!tok->varId() && tok->hasKnownIntValue() && MathLib::toString(tok->values().front().intvalue) == rhs)
if (!tok->varId() && tok->hasKnownIntValue() && MathLib::toString(tok->getKnownIntValue()) == rhs)
return true;
return false;
}
Expand Down Expand Up @@ -1524,7 +1524,7 @@ bool isUsedAsBool(const Token* const tok, const Settings& settings)
if (parent->isUnaryOp("*"))
return isUsedAsBool(parent, settings);
if (Token::Match(parent, "==|!=") && (tok->astSibling()->isNumber() || tok->astSibling()->isKeyword()) && tok->astSibling()->hasKnownIntValue() &&
tok->astSibling()->values().front().intvalue == 0)
tok->astSibling()->getKnownIntValue() == 0)
return true;
if (parent->str() == "(" && astIsRHS(tok) && Token::Match(parent->astOperand1(), "if|while"))
return true;
Expand Down Expand Up @@ -1657,10 +1657,10 @@ bool isSameExpression(bool macro, const Token *tok1, const Token *tok2, const Se
const Token* varTok2 = exprTok;
const ValueFlow::Value* value = nullptr;
if (condTok->astOperand1()->hasKnownIntValue()) {
value = &condTok->astOperand1()->values().front();
value = condTok->astOperand1()->getKnownValue(ValueFlow::Value::ValueType::INT);
varTok1 = condTok->astOperand2();
} else if (condTok->astOperand2()->hasKnownIntValue()) {
value = &condTok->astOperand2()->values().front();
value = condTok->astOperand2()->getKnownValue(ValueFlow::Value::ValueType::INT);
varTok1 = condTok->astOperand1();
}
const bool exprIsNot = Token::simpleMatch(exprTok, "!");
Expand Down
2 changes: 1 addition & 1 deletion lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ static std::vector<ValueFlow::Value> getOverrunIndexValues(const Token* tok,
: std::vector<ValueFlow::Value>{};
if (values.empty()) {
if (indexTokens[i]->hasKnownIntValue())
indexValues.push_back(indexTokens[i]->values().front());
indexValues.push_back(*indexTokens[i]->getKnownValue(ValueFlow::Value::ValueType::INT));
else
indexValues.push_back(ValueFlow::Value::unknown());
continue;
Expand Down
10 changes: 5 additions & 5 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ void CheckCondition::checkBadBitmaskCheck()
(parent->str() == "(" && Token::Match(parent->astOperand1(), "if|while")) ||
(parent->str() == "return" && parent->astOperand1() == tok && inBooleanFunction(tok));

const bool isTrue = (tok->astOperand1()->hasKnownIntValue() && tok->astOperand1()->values().front().intvalue != 0) ||
(tok->astOperand2()->hasKnownIntValue() && tok->astOperand2()->values().front().intvalue != 0);
const bool isTrue = (tok->astOperand1()->hasKnownIntValue() && tok->astOperand1()->getKnownIntValue() != 0) ||
(tok->astOperand2()->hasKnownIntValue() && tok->astOperand2()->getKnownIntValue() != 0);

if (isBoolean && isTrue)
badBitmaskCheckError(tok);
Expand All @@ -332,8 +332,8 @@ void CheckCondition::checkBadBitmaskCheck()
if (mTokenizer->hasIfdef(startStop.first, startStop.second))
continue;

const bool isZero1 = (tok->astOperand1()->hasKnownIntValue() && tok->astOperand1()->values().front().intvalue == 0);
const bool isZero2 = (tok->astOperand2()->hasKnownIntValue() && tok->astOperand2()->values().front().intvalue == 0);
const bool isZero1 = (tok->astOperand1()->hasKnownIntValue() && tok->astOperand1()->getKnownIntValue() == 0);
const bool isZero2 = (tok->astOperand2()->hasKnownIntValue() && tok->astOperand2()->getKnownIntValue() == 0);
if (!isZero1 && !isZero2)
continue;

Expand Down Expand Up @@ -370,7 +370,7 @@ void CheckCondition::comparison()
continue;
if (expr1->hasKnownIntValue())
std::swap(expr1,expr2);
if (!expr2->hasKnownIntValue())
if (!expr2->hasKnownIntValue()) // TODO always false
continue;
if (!compareTokenFlags(expr1, expr2, /*macro*/ true))
continue;
Expand Down
4 changes: 2 additions & 2 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
tok = tok->next();
while (Token::Match(tok, "%name% ::|.") || (startparen && Token::Match(tok, "%name% ,")))
tok = tok->tokAt(2);
const bool isnull = tok->hasKnownIntValue() && tok->values().front().intvalue == 0;
const bool isnull = tok->hasKnownIntValue() && tok->getKnownIntValue() == 0;
if (!isnull && tok->varId() && tok->strAt(1) != "[") {
const VarInfo::AllocInfo allocation(arrayDelete ? NEW_ARRAY : NEW, VarInfo::DEALLOC, delTok);
changeAllocStatus(varInfo, allocation, tok, tok);
Expand Down Expand Up @@ -1035,7 +1035,7 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
if (isAddressOf)
arg = arg->next();

const bool isnull = !isAddressOf && (arg->hasKnownIntValue() && arg->values().front().intvalue == 0);
const bool isnull = !isAddressOf && (arg->hasKnownIntValue() && arg->getKnownIntValue() == 0);

// Is variable allocated?
if (!isnull && (!af || af->arg == argNr)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/checknullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void CheckNullPointer::nullConstantDereference()
const Token *argtok = args[argnr];
if (!argtok->hasKnownIntValue())
continue;
if (argtok->values().front().intvalue != 0)
if (argtok->getKnownIntValue() != 0)
continue;
if (mSettings->library.isnullargbad(tok, argnr+1))
nullPointerError(argtok);
Expand Down
2 changes: 1 addition & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void CheckOther::warningOldStylePointerCast()
tok = tok->next();

const Token *p = tok->tokAt(4);
if (p->hasKnownIntValue() && p->values().front().intvalue==0) // Casting nullpointers is safe
if (p->hasKnownIntValue() && p->getKnownIntValue()==0) // Casting nullpointers is safe
continue;

if (typeTok->tokType() == Token::eType || typeTok->tokType() == Token::eName)
Expand Down
8 changes: 4 additions & 4 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ bool CheckStl::isContainerSizeGE(const Token * containerToken, const Token *expr
mul = expr->astOperand1();
else
return false;
return mul && (!mul->hasKnownIntValue() || mul->values().front().intvalue != 0);
return mul && (!mul->hasKnownIntValue() || mul->getKnownIntValue() != 0);
}
if (expr->str() == "+") {
const Token *op;
Expand Down Expand Up @@ -2491,7 +2491,7 @@ void CheckStl::checkDereferenceInvalidIterator2()
if (cValue && cValue->intvalue == 0) {
if (Token::Match(tok->astParent(), "+|-") && astIsIntegral(tok->astSibling(), false)) {
if (tok->astSibling() && tok->astSibling()->hasKnownIntValue()) {
if (tok->astSibling()->values().front().intvalue == 0)
if (tok->astSibling()->getKnownIntValue() == 0)
continue;
} else {
advanceIndex = tok->astSibling();
Expand Down Expand Up @@ -2840,8 +2840,8 @@ namespace {
alwaysFalse = false;
return;
}
(returnTok->values().front().intvalue ? alwaysTrue : alwaysFalse) &= true;
(returnTok->values().front().intvalue ? alwaysFalse : alwaysTrue) &= false;
(returnTok->getKnownIntValue() ? alwaysTrue : alwaysFalse) &= true;
(returnTok->getKnownIntValue() ? alwaysFalse : alwaysTrue) &= false;
});
if (alwaysTrue == alwaysFalse)
return "";
Expand Down
3 changes: 1 addition & 2 deletions lib/checktype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ void CheckType::checkLongCast()
continue;

if (tok->astOperand2()->hasKnownIntValue()) {
const ValueFlow::Value &v = tok->astOperand2()->values().front();
if (mSettings->platform.isIntValue(v.intvalue))
if (mSettings->platform.isIntValue( tok->astOperand2()->getKnownIntValue()))
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ namespace {
if (!condTok->hasKnownIntValue() || inLoop) {
if (!analyzer->lowerToPossible())
return Break(Analyzer::Terminate::Bail);
} else if (condTok->values().front().intvalue == inElse) {
} else if (condTok->getKnownIntValue() == inElse) {
return Break();
}
// Handle loop
Expand Down
2 changes: 1 addition & 1 deletion lib/fwdanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *
const Token *conditionStart = tok->next();
const Token *condTok = conditionStart->astOperand2();
if (condTok->hasKnownIntValue()) {
const bool cond = !!condTok->values().front().intvalue;
const bool cond = !!condTok->getKnownIntValue();
if (cond) {
FwdAnalysis::Result result = checkRecursive(expr, bodyStart, bodyStart->link(), exprVarIds, local, true, depth);
if (result.type != Result::Type::NONE)
Expand Down
2 changes: 1 addition & 1 deletion lib/pathanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ std::pair<bool, bool> PathAnalysis::checkCond(const Token * tok, bool& known)
{
if (tok->hasKnownIntValue()) {
known = true;
return std::make_pair(!!tok->values().front().intvalue, !tok->values().front().intvalue);
return std::make_pair(!!tok->getKnownIntValue(), !tok->getKnownIntValue());
}
auto it = std::find_if(tok->values().cbegin(), tok->values().cend(), [](const ValueFlow::Value& v) {
return v.isIntValue();
Expand Down
6 changes: 3 additions & 3 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
expr,
[&](const Token* tok) -> std::vector<MathLib::bigint> {
if (tok->hasKnownIntValue())
return {tok->values().front().intvalue};
return {tok->getKnownIntValue()};
MathLib::bigint result = 0;
if (getIntValue(tok->exprId(), result))
return {result};
Expand Down Expand Up @@ -306,7 +306,7 @@ static void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, con
if (!t)
return std::vector<MathLib::bigint>{};
if (t->hasKnownIntValue())
return {t->values().front().intvalue};
return {t->getKnownIntValue()};
MathLib::bigint result = 0;
bool error = false;
execute(t, pm, &result, &error, settings);
Expand Down Expand Up @@ -1372,7 +1372,7 @@ namespace {
if (!expr)
return unknown();
if (expr->hasKnownIntValue() && !expr->isAssignmentOp() && expr->str() != ",")
return expr->values().front();
return *expr->getKnownValue(ValueFlow::Value::ValueType::INT);
if ((value = expr->getKnownValue(ValueFlow::Value::ValueType::FLOAT)) ||
(value = expr->getKnownValue(ValueFlow::Value::ValueType::TOK)) ||
(value = expr->getKnownValue(ValueFlow::Value::ValueType::ITERATOR_START)) ||
Expand Down
8 changes: 8 additions & 0 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,14 @@ bool Token::hasKnownSymbolicValue(const Token* tok) const
});
}

const ValueFlow::Value* Token::getKnownValue() const
{
if (!mImpl->mValues)
return nullptr;
auto it = std::find_if(mImpl->mValues->begin(), mImpl->mValues->end(), std::mem_fn(&ValueFlow::Value::isKnown));
return it == mImpl->mValues->end() ? nullptr : &*it;
}

const ValueFlow::Value* Token::getKnownValue(ValueFlow::Value::ValueType t) const
{
if (!mImpl->mValues)
Expand Down
5 changes: 5 additions & 0 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -1288,8 +1288,13 @@ class CPPCHECKLIB Token {
bool hasKnownValue(ValueFlow::Value::ValueType t) const;
bool hasKnownSymbolicValue(const Token* tok) const;

const ValueFlow::Value* getKnownValue() const;
const ValueFlow::Value* getKnownValue(ValueFlow::Value::ValueType t) const;
MathLib::bigint getKnownIntValue() const {
// TODO: need to perform lookup
assert(!mImpl->mValues->empty());
assert(mImpl->mValues->front().isKnown());
assert(mImpl->mValues->front().valueType == ValueFlow::Value::ValueType::INT);
return mImpl->mValues->front().intvalue;
}

Expand Down
Loading

0 comments on commit 516a7f3

Please sign in to comment.