Skip to content

Commit 3ea0ea1

Browse files
committed
vf_settokenvalue.cpp: actually use the known value in setTokenValue()
1 parent 6fce913 commit 3ea0ea1

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

lib/token.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,18 @@ bool Token::hasKnownSymbolicValue(const Token* tok) const
25452545
});
25462546
}
25472547

2548+
const ValueFlow::Value* Token::getKnownValue() const
2549+
{
2550+
if (!mImpl->mValues)
2551+
return nullptr;
2552+
if (mImpl->mValues->empty())
2553+
return nullptr;
2554+
auto it = std::find_if(mImpl->mValues->begin(), mImpl->mValues->end(), [&](const ValueFlow::Value& value) {
2555+
return value.isKnown();
2556+
});
2557+
return it == mImpl->mValues->end() ? nullptr : &*it;
2558+
}
2559+
25482560
const ValueFlow::Value* Token::getKnownValue(ValueFlow::Value::ValueType t) const
25492561
{
25502562
if (!mImpl->mValues)

lib/token.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,7 @@ class CPPCHECKLIB Token {
13211321
bool hasKnownValue(ValueFlow::Value::ValueType t) const;
13221322
bool hasKnownSymbolicValue(const Token* tok) const;
13231323

1324+
const ValueFlow::Value* getKnownValue() const;
13241325
const ValueFlow::Value* getKnownValue(ValueFlow::Value::ValueType t) const;
13251326
MathLib::bigint getKnownIntValue() const {
13261327
assert(!mImpl->mValues->empty());

lib/vf_settokenvalue.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,10 @@ namespace ValueFlow
391391

392392
else if (parent->str() == "?" && tok->str() == ":" && tok == parent->astOperand2() && parent->astOperand1()) {
393393
// is condition always true/false?
394-
if (parent->astOperand1()->hasKnownValue()) {
395-
const Value &condvalue = parent->astOperand1()->values().front();
396-
assert(condvalue.isKnown());
397-
const bool cond(condvalue.isTokValue() || (condvalue.isIntValue() && condvalue.intvalue != 0));
394+
if (const Value* condvalue = parent->astOperand1()->getKnownValue()) {
395+
const bool cond(condvalue->isTokValue() || (condvalue->isIntValue() && condvalue->intvalue != 0));
398396
if (cond && !tok->astOperand1()) { // true condition, no second operator
399-
setTokenValue(parent, condvalue, settings);
397+
setTokenValue(parent, *condvalue, settings);
400398
} else {
401399
const Token *op = cond ? tok->astOperand1() : tok->astOperand2();
402400
if (!op) // #7769 segmentation fault at setTokenValue()

0 commit comments

Comments
 (0)