Skip to content

Commit 37f4de2

Browse files
authored
fixed #13513 - adjusted access of known int values (danmar#7181)
1 parent 6b1b18b commit 37f4de2

19 files changed

+110
-95
lines changed

lib/astutils.cpp

Lines changed: 8 additions & 8 deletions
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;
@@ -1656,11 +1656,11 @@ bool isSameExpression(bool macro, const Token *tok1, const Token *tok2, const Se
16561656
const Token* varTok1 = nullptr;
16571657
const Token* varTok2 = exprTok;
16581658
const ValueFlow::Value* value = nullptr;
1659-
if (condTok->astOperand1()->hasKnownIntValue()) {
1660-
value = &condTok->astOperand1()->values().front();
1659+
if (const ValueFlow::Value* vi1 = condTok->astOperand1()->getKnownValue(ValueFlow::Value::ValueType::INT)) {
1660+
value = vi1;
16611661
varTok1 = condTok->astOperand2();
1662-
} else if (condTok->astOperand2()->hasKnownIntValue()) {
1663-
value = &condTok->astOperand2()->values().front();
1662+
} else if (const ValueFlow::Value* vi2 = condTok->astOperand2()->getKnownValue(ValueFlow::Value::ValueType::INT)) {
1663+
value = vi2;
16641664
varTok1 = condTok->astOperand1();
16651665
}
16661666
const bool exprIsNot = Token::simpleMatch(exprTok, "!");
@@ -3715,8 +3715,8 @@ static std::set<MathLib::bigint> getSwitchValues(const Token *startbrace, bool &
37153715
}
37163716
if (Token::simpleMatch(tok, "case")) {
37173717
const Token *valueTok = tok->astOperand1();
3718-
if (valueTok->hasKnownIntValue())
3719-
values.insert(valueTok->getKnownIntValue());
3718+
if (const ValueFlow::Value* v = valueTok->getKnownValue(ValueFlow::Value::ValueType::INT))
3719+
values.insert(v->intvalue);
37203720
continue;
37213721
}
37223722
}

lib/checkbufferoverrun.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ static std::vector<ValueFlow::Value> getOverrunIndexValues(const Token* tok,
267267
? ValueFlow::isOutOfBounds(makeSizeValue(size, path), indexTokens[i])
268268
: std::vector<ValueFlow::Value>{};
269269
if (values.empty()) {
270-
if (indexTokens[i]->hasKnownIntValue())
271-
indexValues.push_back(indexTokens[i]->values().front());
270+
if (const ValueFlow::Value* v = indexTokens[i]->getKnownValue(ValueFlow::Value::ValueType::INT))
271+
indexValues.push_back(*v);
272272
else
273273
indexValues.push_back(ValueFlow::Value::unknown());
274274
continue;
@@ -1065,12 +1065,10 @@ void CheckBufferOverrun::objectIndex()
10651065
const Token *idx = tok->astOperand2();
10661066
if (!idx || !obj)
10671067
continue;
1068-
if (idx->hasKnownIntValue()) {
1069-
if (idx->getKnownIntValue() == 0)
1068+
if (const ValueFlow::Value* v = idx->getKnownValue(ValueFlow::Value::ValueType::INT)) {
1069+
if (v->intvalue == 0)
10701070
continue;
10711071
}
1072-
if (idx->hasKnownIntValue() && idx->getKnownIntValue() == 0)
1073-
continue;
10741072

10751073
std::vector<ValueFlow::Value> values = ValueFlow::getLifetimeObjValues(obj, false, -1);
10761074
for (const ValueFlow::Value& v:values) {

lib/checkcondition.cpp

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

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

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

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

@@ -1700,7 +1700,7 @@ void CheckCondition::checkInvalidTestForOverflow()
17001700
const Token * const other = expr->astSibling();
17011701

17021702
// x [+-] c cmp x
1703-
if ((other->isNumber() && other->getKnownIntValue() > 0) ||
1703+
if ((other->isNumber() && other->hasKnownIntValue() && other->getKnownIntValue() > 0) ||
17041704
(!other->isNumber() && other->valueType() && other->valueType()->isIntegral() && other->valueType()->sign == ValueType::Sign::UNSIGNED)) {
17051705
bool result;
17061706
if (lhs->str() == "+")

lib/checkleakautovar.cpp

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ void CheckNullPointer::nullConstantDereference()
392392
const Token *argtok = args[argnr];
393393
if (!argtok->hasKnownIntValue())
394394
continue;
395-
if (argtok->values().front().intvalue != 0)
395+
if (argtok->getKnownIntValue() != 0)
396396
continue;
397397
if (mSettings->library.isnullargbad(tok, argnr+1))
398398
nullPointerError(argtok);

lib/checkother.cpp

Lines changed: 3 additions & 3 deletions
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)
@@ -1399,8 +1399,8 @@ static bool isLargeContainer(const Variable* var, const Settings* settings)
13991399
const std::size_t maxByValueSize = 2 * settings->platform.sizeof_pointer;
14001400
if (var->dimensions().empty()) {
14011401
if (vt->container->startPattern == "std :: bitset <") {
1402-
if (vt->containerTypeToken->hasKnownIntValue())
1403-
return vt->containerTypeToken->getKnownIntValue() / 8 > maxByValueSize;
1402+
if (const ValueFlow::Value* v = vt->containerTypeToken->getKnownValue(ValueFlow::Value::ValueType::INT))
1403+
return v->intvalue / 8 > maxByValueSize;
14041404
}
14051405
return false;
14061406
}

lib/checkstl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ bool CheckStl::isContainerSizeGE(const Token * containerToken, const Token *expr
304304
mul = expr->astOperand1();
305305
else
306306
return false;
307-
return mul && (!mul->hasKnownIntValue() || mul->values().front().intvalue != 0);
307+
return mul && (!mul->hasKnownIntValue() || mul->getKnownIntValue() != 0);
308308
}
309309
if (expr->str() == "+") {
310310
const Token *op;
@@ -2492,7 +2492,7 @@ void CheckStl::checkDereferenceInvalidIterator2()
24922492
if (cValue && cValue->intvalue == 0) {
24932493
if (Token::Match(tok->astParent(), "+|-") && astIsIntegral(tok->astSibling(), false)) {
24942494
if (tok->astSibling() && tok->astSibling()->hasKnownIntValue()) {
2495-
if (tok->astSibling()->values().front().intvalue == 0)
2495+
if (tok->astSibling()->getKnownIntValue() == 0)
24962496
continue;
24972497
} else {
24982498
advanceIndex = tok->astSibling();
@@ -2906,8 +2906,8 @@ namespace {
29062906
alwaysFalse = false;
29072907
return;
29082908
}
2909-
(returnTok->values().front().intvalue ? alwaysTrue : alwaysFalse) &= true;
2910-
(returnTok->values().front().intvalue ? alwaysFalse : alwaysTrue) &= false;
2909+
(returnTok->getKnownIntValue() ? alwaysTrue : alwaysFalse) &= true;
2910+
(returnTok->getKnownIntValue() ? alwaysFalse : alwaysTrue) &= false;
29112911
});
29122912
if (alwaysTrue == alwaysFalse)
29132913
return "";

lib/checktype.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,8 @@ void CheckType::checkLongCast()
344344
if (tok->str() != "=" || !Token::Match(tok->astOperand2(), "*|<<") || tok->astOperand2()->isUnaryOp("*"))
345345
continue;
346346

347-
if (tok->astOperand2()->hasKnownIntValue()) {
348-
const ValueFlow::Value &v = tok->astOperand2()->values().front();
349-
if (mSettings->platform.isIntValue(v.intvalue))
347+
if (const ValueFlow::Value* v = tok->astOperand2()->getKnownValue(ValueFlow::Value::ValueType::INT)) {
348+
if (mSettings->platform.isIntValue(v->intvalue))
350349
continue;
351350
}
352351

lib/checkuninitvar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ static void conditionAlwaysTrueOrFalse(const Token *tok, const std::map<nonneg i
292292
if (!tok)
293293
return;
294294

295-
if (tok->hasKnownIntValue()) {
296-
if (tok->getKnownIntValue() == 0)
295+
if (const ValueFlow::Value* v = tok->getKnownValue(ValueFlow::Value::ValueType::INT)) {
296+
if (v->intvalue == 0)
297297
*alwaysFalse = true;
298298
else
299299
*alwaysTrue = true;

lib/forwardanalyzer.cpp

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)