Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 5, 2024
1 parent e4087cf commit a52a4d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7124,13 +7124,14 @@ static void valueFlowContainerSize(const TokenList& tokenlist,
}
for (const ValueFlow::Value& value : values)
setTokenValue(tok, value, settings);
} else if (Token::Match(tok, "%name%|;|{|}|> %var% = ") && Token::Match(tok->tokAt(2)->astOperand2(), "[({]") && settings.library.detectContainer(tok->tokAt(3))) {
} else if (Token::Match(tok, "%name%|;|{|}|> %var% = ") && Token::Match(tok->tokAt(2)->astOperand2(), "[({]") &&
(tok->tokAt(3) == tok->tokAt(2)->astOperand2() || settings.library.detectContainer(tok->tokAt(3)))) {
Token* containerTok = tok->next();
if (containerTok->exprId() == 0)
continue;
if (astIsContainer(containerTok) && containerTok->valueType()->container->size_templateArgNo < 0) {
Token* rhs = tok->tokAt(2)->astOperand2();
std::vector<ValueFlow::Value> values = getContainerSizeFromConstructor(rhs, containerTok->valueType(), settings);
std::vector<ValueFlow::Value> values = getInitListSize(rhs, containerTok->valueType(), settings);
valueFlowContainerSetTokValue(tokenlist, errorLogger, settings, containerTok, rhs);
for (const ValueFlow::Value& value : values)
valueFlowForward(containerTok->next(), containerTok, value, tokenlist, errorLogger, settings);
Expand Down
16 changes: 16 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6926,6 +6926,22 @@ class TestValueFlow : public TestFixture {
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 4U, 1));

code = "int f() {\n"
" std::string s;\n"
" s.append(\"0\");\n"
" auto x = s.size();\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 1));

code = "int f() {\n"
" std::string s;\n"
" s = std::string(\"0\");\n"
" auto x = s.size();\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 1));
}

void valueFlowContainerElement()
Expand Down

0 comments on commit a52a4d0

Please sign in to comment.