From a5cfa85e0d29e4e668ea3bd4ff3ddd7cdfdc70ec Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sun, 20 Aug 2023 15:01:04 -0500 Subject: [PATCH] Fix 11884: Hang in valueFlowGetStrLength (#5352) --- lib/valueflow.cpp | 10 ++++++---- test/testvalueflow.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ffd9c2095..c650e12b6 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -8707,10 +8707,12 @@ static MathLib::bigint valueFlowGetStrLength(const Token* tok) return Token::getStrLength(tok); if (astIsGenericChar(tok) || tok->tokType() == Token::eChar) return 1; - if (const ValueFlow::Value* v2 = tok->getKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE)) - return v2->intvalue; - if (const ValueFlow::Value* v1 = tok->getKnownValue(ValueFlow::Value::ValueType::TOK)) - return valueFlowGetStrLength(v1->tokvalue); + if (const ValueFlow::Value* v = tok->getKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE)) + return v->intvalue; + if (const ValueFlow::Value* v = tok->getKnownValue(ValueFlow::Value::ValueType::TOK)) { + if (v->tokvalue != tok) + return valueFlowGetStrLength(v->tokvalue); + } return 0; } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index f24942164..8a075721a 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -7397,6 +7397,18 @@ private: " }\n" "}"; valueOfTok(code, "path"); + + code = "struct S {\n" + " std::string to_string() const {\n" + " return { this->p , (size_t)this->n };\n" + " }\n" + " const char* p;\n" + " int n;\n" + "};\n" + "void f(S s, std::string& str) {\n" + " str += s.to_string();\n" + "}\n"; + valueOfTok(code, "s"); } void valueFlowUnknownMixedOperators() {