Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6884,6 +6884,26 @@ static void valueFlowContainerSize(const TokenList& tokenlist,
}
for (const ValueFlow::Value& value : values)
setTokenValue(tok, value, settings);
}
else if (Token::Match(tok->previous(), ",|( {")) {
int nArg{};
if (const Token* funcTok = getTokenArgumentFunction(tok, nArg)) {
if (const Function* func = funcTok->function()) {
if (const Variable* var = func->getArgumentVar(nArg)) {
if (var->valueType() && var->valueType()->container && var->valueType()->container->size_templateArgNo < 0) {
std::vector<ValueFlow::Value> values = getInitListSize(tok, var->valueType(), settings, true);
ValueFlow::Value tokValue;
tokValue.valueType = ValueFlow::Value::ValueType::TOK;
tokValue.tokvalue = tok;
tokValue.setKnown();
values.push_back(std::move(tokValue));

for (const ValueFlow::Value &value : values)
setTokenValue(tok, value, settings);
}
}
}
}
} else if (Token::Match(tok, ";|{|} %var% =") && Token::Match(tok->tokAt(2)->astOperand2(), "[({]") &&
// init list
((tok->tokAt(2) == tok->tokAt(2)->astOperand2()->astParent() && !tok->tokAt(2)->astOperand2()->astOperand2() && tok->tokAt(2)->astOperand2()->str() == "{") ||
Expand Down
22 changes: 22 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,28 @@ class TestStl : public TestFixture {
" (void)v[0];\n"
"}\n");
ASSERT_EQUALS("", errout_str());

checkNormal("int f(const std::vector<int>& v) {\n" // #8983
" return v[2];\n"
"}\n"
"int g() {\n"
" return f({});\n"
"}\n"
"int h() {\n"
" return f({ 1, 2 });\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:13]: error: Out of bounds access in 'v[2]', if 'v' size is 2 and '2' is 2 [containerOutOfBounds]\n"
"[test.cpp:2:13]: error: Out of bounds access in expression 'v[2]' because 'v' is empty. [containerOutOfBounds]\n",
errout_str());

checkNormal("int f(int x, const std::vector<int>& v) {\n"
" return x + v[0];\n"
"}\n"
"int g() {\n"
" return f(1, {});\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:17]: error: Out of bounds access in expression 'v[0]' because 'v' is empty. [containerOutOfBounds]\n",
errout_str());
}

void outOfBoundsSymbolic()
Expand Down
Loading