Skip to content

Commit a30d718

Browse files
Partial fix for #12046 FN: containerOutOfBounds (temporary string passed to subfunction) (danmar#8083)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 46c8934 commit a30d718

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/valueflow.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6706,13 +6706,15 @@ static void valueFlowContainerSize(const TokenList& tokenlist,
67066706
for (const ValueFlow::Value& value : values)
67076707
setTokenValue(tok, value, settings);
67086708
}
6709-
else if (Token::Match(tok->previous(), ",|( {")) {
6709+
else if (Token::Match(tok->previous(), ",|( {|%str%")) {
67106710
int nArg{};
67116711
if (const Token* funcTok = getTokenArgumentFunction(tok, nArg)) {
67126712
if (const Function* func = funcTok->function()) {
67136713
if (const Variable* var = func->getArgumentVar(nArg)) {
67146714
if (var->valueType() && var->valueType()->container && var->valueType()->container->size_templateArgNo < 0) {
6715-
std::vector<ValueFlow::Value> values = getInitListSize(tok, var->valueType(), settings, true);
6715+
auto values = tok->tokType() == Token::Type::eString
6716+
? std::vector<ValueFlow::Value>{makeContainerSizeValue(Token::getStrLength(tok))}
6717+
: getInitListSize(tok, var->valueType(), settings, true);
67166718
ValueFlow::Value tokValue;
67176719
tokValue.valueType = ValueFlow::Value::ValueType::TOK;
67186720
tokValue.tokvalue = tok;

test/teststl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,11 @@ class TestStl : public TestFixture {
967967
"}\n");
968968
ASSERT_EQUALS("[test.cpp:2:17]: error: Out of bounds access in expression 'v[0]' because 'v' is empty. [containerOutOfBounds]\n",
969969
errout_str());
970+
971+
checkNormal("bool f(const std::string_view s) { return s[500] == 'x'; }\n" // #12046
972+
"bool g() { return f(\" \"); }\n");
973+
ASSERT_EQUALS("[test.cpp:1:44]: error: Out of bounds access in 's[500]', if 's' size is 1 and '500' is 500 [containerOutOfBounds]\n",
974+
errout_str());
970975
}
971976

972977
void outOfBoundsSymbolic()

0 commit comments

Comments
 (0)