Skip to content

Commit 516a7f3

Browse files
committed
fixed #13513 - corrected access of known int values [skip ci]
1 parent 9871a09 commit 516a7f3

17 files changed

+64
-52
lines changed

lib/astutils.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ static bool match(const Token *tok, const std::string &rhs)
360360
{
361361
if (tok->str() == rhs)
362362
return true;
363-
if (!tok->varId() && tok->hasKnownIntValue() && MathLib::toString(tok->values().front().intvalue) == rhs)
363+
if (!tok->varId() && tok->hasKnownIntValue() && MathLib::toString(tok->getKnownIntValue()) == rhs)
364364
return true;
365365
return false;
366366
}
@@ -1524,7 +1524,7 @@ bool isUsedAsBool(const Token* const tok, const Settings& settings)
15241524
if (parent->isUnaryOp("*"))
15251525
return isUsedAsBool(parent, settings);
15261526
if (Token::Match(parent, "==|!=") && (tok->astSibling()->isNumber() || tok->astSibling()->isKeyword()) && tok->astSibling()->hasKnownIntValue() &&
1527-
tok->astSibling()->values().front().intvalue == 0)
1527+
tok->astSibling()->getKnownIntValue() == 0)
15281528
return true;
15291529
if (parent->str() == "(" && astIsRHS(tok) && Token::Match(parent->astOperand1(), "if|while"))
15301530
return true;
@@ -1657,10 +1657,10 @@ bool isSameExpression(bool macro, const Token *tok1, const Token *tok2, const Se
16571657
const Token* varTok2 = exprTok;
16581658
const ValueFlow::Value* value = nullptr;
16591659
if (condTok->astOperand1()->hasKnownIntValue()) {
1660-
value = &condTok->astOperand1()->values().front();
1660+
value = condTok->astOperand1()->getKnownValue(ValueFlow::Value::ValueType::INT);
16611661
varTok1 = condTok->astOperand2();
16621662
} else if (condTok->astOperand2()->hasKnownIntValue()) {
1663-
value = &condTok->astOperand2()->values().front();
1663+
value = condTok->astOperand2()->getKnownValue(ValueFlow::Value::ValueType::INT);
16641664
varTok1 = condTok->astOperand1();
16651665
}
16661666
const bool exprIsNot = Token::simpleMatch(exprTok, "!");

lib/checkbufferoverrun.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ static std::vector<ValueFlow::Value> getOverrunIndexValues(const Token* tok,
266266
: std::vector<ValueFlow::Value>{};
267267
if (values.empty()) {
268268
if (indexTokens[i]->hasKnownIntValue())
269-
indexValues.push_back(indexTokens[i]->values().front());
269+
indexValues.push_back(*indexTokens[i]->getKnownValue(ValueFlow::Value::ValueType::INT));
270270
else
271271
indexValues.push_back(ValueFlow::Value::unknown());
272272
continue;

lib/checkcondition.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ void CheckCondition::checkBadBitmaskCheck()
321321
(parent->str() == "(" && Token::Match(parent->astOperand1(), "if|while")) ||
322322
(parent->str() == "return" && parent->astOperand1() == tok && inBooleanFunction(tok));
323323

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

327327
if (isBoolean && isTrue)
328328
badBitmaskCheckError(tok);
@@ -332,8 +332,8 @@ void CheckCondition::checkBadBitmaskCheck()
332332
if (mTokenizer->hasIfdef(startStop.first, startStop.second))
333333
continue;
334334

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

@@ -370,7 +370,7 @@ void CheckCondition::comparison()
370370
continue;
371371
if (expr1->hasKnownIntValue())
372372
std::swap(expr1,expr2);
373-
if (!expr2->hasKnownIntValue())
373+
if (!expr2->hasKnownIntValue()) // TODO always false
374374
continue;
375375
if (!compareTokenFlags(expr1, expr2, /*macro*/ true))
376376
continue;

lib/checkleakautovar.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
718718
tok = tok->next();
719719
while (Token::Match(tok, "%name% ::|.") || (startparen && Token::Match(tok, "%name% ,")))
720720
tok = tok->tokAt(2);
721-
const bool isnull = tok->hasKnownIntValue() && tok->values().front().intvalue == 0;
721+
const bool isnull = tok->hasKnownIntValue() && tok->getKnownIntValue() == 0;
722722
if (!isnull && tok->varId() && tok->strAt(1) != "[") {
723723
const VarInfo::AllocInfo allocation(arrayDelete ? NEW_ARRAY : NEW, VarInfo::DEALLOC, delTok);
724724
changeAllocStatus(varInfo, allocation, tok, tok);
@@ -1035,7 +1035,7 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
10351035
if (isAddressOf)
10361036
arg = arg->next();
10371037

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

10401040
// Is variable allocated?
10411041
if (!isnull && (!af || af->arg == argNr)) {

lib/checknullpointer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ void CheckNullPointer::nullConstantDereference()
391391
const Token *argtok = args[argnr];
392392
if (!argtok->hasKnownIntValue())
393393
continue;
394-
if (argtok->values().front().intvalue != 0)
394+
if (argtok->getKnownIntValue() != 0)
395395
continue;
396396
if (mSettings->library.isnullargbad(tok, argnr+1))
397397
nullPointerError(argtok);

lib/checkother.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ void CheckOther::warningOldStylePointerCast()
347347
tok = tok->next();
348348

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

353353
if (typeTok->tokType() == Token::eType || typeTok->tokType() == Token::eName)

lib/checkstl.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ bool CheckStl::isContainerSizeGE(const Token * containerToken, const Token *expr
303303
mul = expr->astOperand1();
304304
else
305305
return false;
306-
return mul && (!mul->hasKnownIntValue() || mul->values().front().intvalue != 0);
306+
return mul && (!mul->hasKnownIntValue() || mul->getKnownIntValue() != 0);
307307
}
308308
if (expr->str() == "+") {
309309
const Token *op;
@@ -2491,7 +2491,7 @@ void CheckStl::checkDereferenceInvalidIterator2()
24912491
if (cValue && cValue->intvalue == 0) {
24922492
if (Token::Match(tok->astParent(), "+|-") && astIsIntegral(tok->astSibling(), false)) {
24932493
if (tok->astSibling() && tok->astSibling()->hasKnownIntValue()) {
2494-
if (tok->astSibling()->values().front().intvalue == 0)
2494+
if (tok->astSibling()->getKnownIntValue() == 0)
24952495
continue;
24962496
} else {
24972497
advanceIndex = tok->astSibling();
@@ -2840,8 +2840,8 @@ namespace {
28402840
alwaysFalse = false;
28412841
return;
28422842
}
2843-
(returnTok->values().front().intvalue ? alwaysTrue : alwaysFalse) &= true;
2844-
(returnTok->values().front().intvalue ? alwaysFalse : alwaysTrue) &= false;
2843+
(returnTok->getKnownIntValue() ? alwaysTrue : alwaysFalse) &= true;
2844+
(returnTok->getKnownIntValue() ? alwaysFalse : alwaysTrue) &= false;
28452845
});
28462846
if (alwaysTrue == alwaysFalse)
28472847
return "";

lib/checktype.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ void CheckType::checkLongCast()
344344
continue;
345345

346346
if (tok->astOperand2()->hasKnownIntValue()) {
347-
const ValueFlow::Value &v = tok->astOperand2()->values().front();
348-
if (mSettings->platform.isIntValue(v.intvalue))
347+
if (mSettings->platform.isIntValue( tok->astOperand2()->getKnownIntValue()))
349348
continue;
350349
}
351350

lib/forwardanalyzer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ namespace {
617617
if (!condTok->hasKnownIntValue() || inLoop) {
618618
if (!analyzer->lowerToPossible())
619619
return Break(Analyzer::Terminate::Bail);
620-
} else if (condTok->values().front().intvalue == inElse) {
620+
} else if (condTok->getKnownIntValue() == inElse) {
621621
return Break();
622622
}
623623
// Handle loop

lib/fwdanalysis.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *
224224
const Token *conditionStart = tok->next();
225225
const Token *condTok = conditionStart->astOperand2();
226226
if (condTok->hasKnownIntValue()) {
227-
const bool cond = !!condTok->values().front().intvalue;
227+
const bool cond = !!condTok->getKnownIntValue();
228228
if (cond) {
229229
FwdAnalysis::Result result = checkRecursive(expr, bodyStart, bodyStart->link(), exprVarIds, local, true, depth);
230230
if (result.type != Result::Type::NONE)

lib/pathanalysis.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ std::pair<bool, bool> PathAnalysis::checkCond(const Token * tok, bool& known)
5050
{
5151
if (tok->hasKnownIntValue()) {
5252
known = true;
53-
return std::make_pair(!!tok->values().front().intvalue, !tok->values().front().intvalue);
53+
return std::make_pair(!!tok->getKnownIntValue(), !tok->getKnownIntValue());
5454
}
5555
auto it = std::find_if(tok->values().cbegin(), tok->values().cend(), [](const ValueFlow::Value& v) {
5656
return v.isIntValue();

lib/programmemory.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
6565
expr,
6666
[&](const Token* tok) -> std::vector<MathLib::bigint> {
6767
if (tok->hasKnownIntValue())
68-
return {tok->values().front().intvalue};
68+
return {tok->getKnownIntValue()};
6969
MathLib::bigint result = 0;
7070
if (getIntValue(tok->exprId(), result))
7171
return {result};
@@ -306,7 +306,7 @@ static void programMemoryParseCondition(ProgramMemory& pm, const Token* tok, con
306306
if (!t)
307307
return std::vector<MathLib::bigint>{};
308308
if (t->hasKnownIntValue())
309-
return {t->values().front().intvalue};
309+
return {t->getKnownIntValue()};
310310
MathLib::bigint result = 0;
311311
bool error = false;
312312
execute(t, pm, &result, &error, settings);
@@ -1372,7 +1372,7 @@ namespace {
13721372
if (!expr)
13731373
return unknown();
13741374
if (expr->hasKnownIntValue() && !expr->isAssignmentOp() && expr->str() != ",")
1375-
return expr->values().front();
1375+
return *expr->getKnownValue(ValueFlow::Value::ValueType::INT);
13761376
if ((value = expr->getKnownValue(ValueFlow::Value::ValueType::FLOAT)) ||
13771377
(value = expr->getKnownValue(ValueFlow::Value::ValueType::TOK)) ||
13781378
(value = expr->getKnownValue(ValueFlow::Value::ValueType::ITERATOR_START)) ||

lib/token.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,14 @@ bool Token::hasKnownSymbolicValue(const Token* tok) const
25082508
});
25092509
}
25102510

2511+
const ValueFlow::Value* Token::getKnownValue() const
2512+
{
2513+
if (!mImpl->mValues)
2514+
return nullptr;
2515+
auto it = std::find_if(mImpl->mValues->begin(), mImpl->mValues->end(), std::mem_fn(&ValueFlow::Value::isKnown));
2516+
return it == mImpl->mValues->end() ? nullptr : &*it;
2517+
}
2518+
25112519
const ValueFlow::Value* Token::getKnownValue(ValueFlow::Value::ValueType t) const
25122520
{
25132521
if (!mImpl->mValues)

lib/token.h

+5
Original file line numberDiff line numberDiff line change
@@ -1288,8 +1288,13 @@ class CPPCHECKLIB Token {
12881288
bool hasKnownValue(ValueFlow::Value::ValueType t) const;
12891289
bool hasKnownSymbolicValue(const Token* tok) const;
12901290

1291+
const ValueFlow::Value* getKnownValue() const;
12911292
const ValueFlow::Value* getKnownValue(ValueFlow::Value::ValueType t) const;
12921293
MathLib::bigint getKnownIntValue() const {
1294+
// TODO: need to perform lookup
1295+
assert(!mImpl->mValues->empty());
1296+
assert(mImpl->mValues->front().isKnown());
1297+
assert(mImpl->mValues->front().valueType == ValueFlow::Value::ValueType::INT);
12931298
return mImpl->mValues->front().intvalue;
12941299
}
12951300

0 commit comments

Comments
 (0)