Skip to content

Commit

Permalink
Fix #12987 false negative: knownConditionTrueFalse with std::string (…
Browse files Browse the repository at this point in the history
…regression) (danmar#6661)
  • Loading branch information
chrchr-github committed Aug 5, 2024
1 parent 1a850d9 commit de83fc7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6909,7 +6909,8 @@ static std::vector<ValueFlow::Value> 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])) {
Expand Down
7 changes: 7 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit de83fc7

Please sign in to comment.