Skip to content

Commit 4fdcb0c

Browse files
Fix #11649 Hang in setTokenValue() on huge array (#5010)
* Fix #11649 Hang in setTokenValue() on huge array * Fix function call
1 parent 023e79b commit 4fdcb0c

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

lib/valueflow.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ static ValueFlow::Value truncateImplicitConversion(Token* parent, const ValueFlo
604604
static void setTokenValue(Token* tok,
605605
ValueFlow::Value value,
606606
const Settings* settings,
607+
bool isInitList = false,
607608
SourceLocation loc = SourceLocation::current())
608609
{
609610
// Skip setting values that are too big since its ambiguous
@@ -627,7 +628,7 @@ static void setTokenValue(Token* tok,
627628
if (!parent)
628629
return;
629630

630-
if (Token::simpleMatch(parent, ",") && astIsRHS(tok)) {
631+
if (!isInitList && Token::simpleMatch(parent, ",") && astIsRHS(tok)) {
631632
const Token* callParent = findParent(parent, [](const Token* p) {
632633
return !Token::simpleMatch(p, ",");
633634
});
@@ -1137,7 +1138,7 @@ size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings *settings)
11371138
static bool getMinMaxValues(const ValueType* vt, const cppcheck::Platform& platform, MathLib::bigint& minValue, MathLib::bigint& maxValue);
11381139

11391140
// Handle various constants..
1140-
static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, bool cpp)
1141+
static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, bool cpp, bool isInitList = false)
11411142
{
11421143
if ((tok->isNumber() && MathLib::isInt(tok->str())) || (tok->tokType() == Token::eChar)) {
11431144
try {
@@ -1151,7 +1152,7 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
11511152
ValueFlow::Value value(signedValue);
11521153
if (!tok->isTemplateArg())
11531154
value.setKnown();
1154-
setTokenValue(tok, std::move(value), settings);
1155+
setTokenValue(tok, std::move(value), settings, isInitList);
11551156
} catch (const std::exception & /*e*/) {
11561157
// Bad character literal
11571158
}
@@ -1161,17 +1162,17 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
11611162
value.floatValue = MathLib::toDoubleNumber(tok->str());
11621163
if (!tok->isTemplateArg())
11631164
value.setKnown();
1164-
setTokenValue(tok, std::move(value), settings);
1165+
setTokenValue(tok, std::move(value), settings, isInitList);
11651166
} else if (tok->enumerator() && tok->enumerator()->value_known) {
11661167
ValueFlow::Value value(tok->enumerator()->value);
11671168
if (!tok->isTemplateArg())
11681169
value.setKnown();
1169-
setTokenValue(tok, std::move(value), settings);
1170+
setTokenValue(tok, std::move(value), settings, isInitList);
11701171
} else if (tok->str() == "NULL" || (cpp && tok->str() == "nullptr")) {
11711172
ValueFlow::Value value(0);
11721173
if (!tok->isTemplateArg())
11731174
value.setKnown();
1174-
setTokenValue(tok, std::move(value), settings);
1175+
setTokenValue(tok, std::move(value), settings, isInitList);
11751176
} else if (Token::simpleMatch(tok, "sizeof (")) {
11761177
if (tok->next()->astOperand2() && !tok->next()->astOperand2()->isLiteral() && tok->next()->astOperand2()->valueType() &&
11771178
(tok->next()->astOperand2()->valueType()->pointer == 0 || // <- TODO this is a bailout, abort when there are array->pointer conversions
@@ -1339,8 +1340,16 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
13391340

13401341
static void valueFlowNumber(TokenList *tokenlist, const Settings* settings)
13411342
{
1343+
bool isInitList = false;
1344+
const Token* endInit{};
13421345
for (Token *tok = tokenlist->front(); tok;) {
1343-
tok = valueFlowSetConstantValue(tok, settings, tokenlist->isCPP());
1346+
if (!isInitList && tok->str() == "{" && Token::simpleMatch(tok->astOperand1(), ",")) {
1347+
isInitList = true;
1348+
endInit = tok->link();
1349+
}
1350+
tok = valueFlowSetConstantValue(tok, settings, tokenlist->isCPP(), isInitList);
1351+
if (isInitList && tok == endInit)
1352+
isInitList = false;
13441353
}
13451354

13461355
if (tokenlist->isCPP()) {

0 commit comments

Comments
 (0)