Skip to content

Commit a5cfa85

Browse files
authored
Fix 11884: Hang in valueFlowGetStrLength (#5352)
1 parent 63811b2 commit a5cfa85

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

lib/valueflow.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8707,10 +8707,12 @@ static MathLib::bigint valueFlowGetStrLength(const Token* tok)
87078707
return Token::getStrLength(tok);
87088708
if (astIsGenericChar(tok) || tok->tokType() == Token::eChar)
87098709
return 1;
8710-
if (const ValueFlow::Value* v2 = tok->getKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE))
8711-
return v2->intvalue;
8712-
if (const ValueFlow::Value* v1 = tok->getKnownValue(ValueFlow::Value::ValueType::TOK))
8713-
return valueFlowGetStrLength(v1->tokvalue);
8710+
if (const ValueFlow::Value* v = tok->getKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE))
8711+
return v->intvalue;
8712+
if (const ValueFlow::Value* v = tok->getKnownValue(ValueFlow::Value::ValueType::TOK)) {
8713+
if (v->tokvalue != tok)
8714+
return valueFlowGetStrLength(v->tokvalue);
8715+
}
87148716
return 0;
87158717
}
87168718

test/testvalueflow.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7397,6 +7397,18 @@ class TestValueFlow : public TestFixture {
73977397
" }\n"
73987398
"}";
73997399
valueOfTok(code, "path");
7400+
7401+
code = "struct S {\n"
7402+
" std::string to_string() const {\n"
7403+
" return { this->p , (size_t)this->n };\n"
7404+
" }\n"
7405+
" const char* p;\n"
7406+
" int n;\n"
7407+
"};\n"
7408+
"void f(S s, std::string& str) {\n"
7409+
" str += s.to_string();\n"
7410+
"}\n";
7411+
valueOfTok(code, "s");
74007412
}
74017413

74027414
void valueFlowUnknownMixedOperators() {

0 commit comments

Comments
 (0)