From dbfd3eaef95a6943f87e06ac31320df555e523ce Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 9 Feb 2026 12:58:34 +0100 Subject: [PATCH 1/4] Update valueflow.cpp --- lib/valueflow.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 86b49c0b137..fcf9806ede3 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -6491,6 +6491,9 @@ static std::vector getContainerSizeFromConstructorArgs(const s if (astIsPointer(args[0])) { if (args.size() == 1 && args[0]->tokType() == Token::Type::eString) return {makeContainerSizeValue(Token::getStrLength(args[0]), known)}; + if (args.size() == 1 && args[0]->variable() && args[0]->variable()->isArray() && + args[0]->variable()->isConst() && args[0]->variable()->dimensions().size() == 1) + return {makeContainerSizeValue(args[0]->variable()->dimensions()[0].num, known)}; if (args.size() == 2 && astIsIntegral(args[1], false)) // { char*, count } return {makeContainerSizeValue(args[1], known)}; } else if (astIsContainer(args[0])) { From 9e6f7eb99405d46c012b5a54af70f9f05b2d15b9 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 9 Feb 2026 12:59:51 +0100 Subject: [PATCH 2/4] Update teststl.cpp --- test/teststl.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/teststl.cpp b/test/teststl.cpp index 636fd36b1d0..5b8aac4976b 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -980,6 +980,14 @@ class TestStl : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:4:13]: error: Out of bounds access in 'x[3]', if 'x' size is 1 and '3' is 3 [containerOutOfBounds]\n", errout_str()); + + checkNormal("int main() {\n" + " const char a[] = \"abc\";\n" + " std::string_view x{ a };\n" + " return x[5];\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4:13]: error: Out of bounds access in 'x[5]', if 'x' size is 4 and '5' is 5 [containerOutOfBounds]\n", + errout_str()); } void outOfBoundsSymbolic() From 4c9e107d66b4187e747e5ff65d9fa00e2ea1bb7c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 9 Feb 2026 17:04:39 +0100 Subject: [PATCH 3/4] Update valueflow.cpp --- lib/valueflow.cpp | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index fcf9806ede3..431c58a4541 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -6711,23 +6711,25 @@ static void valueFlowContainerSize(const TokenList& tokenlist, for (const ValueFlow::Value& value : values) setTokenValue(tok, value, settings); } - 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) { - 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; - tokValue.setKnown(); - values.push_back(std::move(tokValue)); - - for (const ValueFlow::Value &value : values) - setTokenValue(tok, value, settings); + else if (Token::Match(tok->previous(), ",|(") && (Token::Match(tok, "{|%str%") || settings.library.detectContainer(tok))) { + if (Token* argTok = tok->previous()->astOperand2()) { + int nArg{}; + if (const Token* funcTok = getTokenArgumentFunction(argTok, 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) { + auto values = argTok->tokType() == Token::Type::eString + ? std::vector{makeContainerSizeValue(Token::getStrLength(argTok))} + : getInitListSize(argTok, var->valueType(), settings, true); + ValueFlow::Value tokValue; + tokValue.valueType = ValueFlow::Value::ValueType::TOK; + tokValue.tokvalue = argTok; + tokValue.setKnown(); + values.push_back(std::move(tokValue)); + + for (const ValueFlow::Value& value : values) + setTokenValue(argTok, value, settings); + } } } } From 289be5055f9540390b079a673937a7249d5e22df Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 9 Feb 2026 17:06:11 +0100 Subject: [PATCH 4/4] Update teststl.cpp --- test/teststl.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/teststl.cpp b/test/teststl.cpp index 5b8aac4976b..c24bf2a5fb1 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -988,6 +988,16 @@ class TestStl : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:4:13]: error: Out of bounds access in 'x[5]', if 'x' size is 4 and '5' is 5 [containerOutOfBounds]\n", errout_str()); + + checkNormal("int f(const std::string& v) {\n" + " return v[2];\n" + "}\n" + "int main() {\n" + " std::string_view x{ \"a\" };\n" + " return f(std::string(x));\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2:13]: error: Out of bounds access in 'v[2]', if 'v' size is 1 and '2' is 2 [containerOutOfBounds]\n", + errout_str()); } void outOfBoundsSymbolic()