From de83fc785fd9b3518059a37c2730b70f095409a7 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 5 Aug 2024 19:29:29 +0200 Subject: [PATCH] Fix #12987 false negative: knownConditionTrueFalse with std::string (regression) (#6661) --- lib/valueflow.cpp | 3 ++- test/testvalueflow.cpp | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 11999c79bb6..72c3fbf588c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -6909,7 +6909,8 @@ static std::vector getContainerSizeFromConstructorArgs(const s } } else if (container->stdStringLike) { if (astIsPointer(args[0])) { - // TODO: Try to read size of string literal { "abc" } + if (args.size() == 1 && args[0]->tokType() == Token::Type::eString) + return {makeContainerSizeValue(Token::getStrLength(args[0]), known)}; if (args.size() == 2 && astIsIntegral(args[1], false)) // { char*, count } return {makeContainerSizeValue(args[1], known)}; } else if (astIsContainer(args[0])) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 84b3a1f13bd..f1250b87ab8 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -6919,6 +6919,13 @@ class TestValueFlow : public TestFixture { " return x;\n" "}\n"; ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 1)); + + code = "int f() {\n" // #12987 + " std::string s{\"0\"};\n" + " auto x = s.size();\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfXKnown(code, 4U, 1)); } void valueFlowContainerElement()