diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index e649786fb8f..f3df5a0c31f 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -6707,13 +6707,15 @@ static void valueFlowContainerSize(const TokenList& tokenlist, for (const ValueFlow::Value& value : values) setTokenValue(tok, value, settings); } - else if (Token::Match(tok->previous(), ",|( {")) { + else if (Token::Match(tok->previous(), ",|( {|%str%")) { 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 values = getInitListSize(tok, var->valueType(), settings, true); + auto values = tok->tokType() == Token::Type::eString + ? std::vector{makeContainerSizeValue(Token::getStrLength(tok))} + : getInitListSize(tok, var->valueType(), settings, true); ValueFlow::Value tokValue; tokValue.valueType = ValueFlow::Value::ValueType::TOK; tokValue.tokvalue = tok; diff --git a/test/teststl.cpp b/test/teststl.cpp index ec6f46148a8..2c4669dbeca 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -967,6 +967,11 @@ class TestStl : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:2:17]: error: Out of bounds access in expression 'v[0]' because 'v' is empty. [containerOutOfBounds]\n", errout_str()); + + checkNormal("bool f(const std::string_view s) { return s[500] == 'x'; }\n" // #12046 + "bool g() { return f(\" \"); }\n"); + 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", + errout_str()); } void outOfBoundsSymbolic()