From 61b87bcc60d38ed7987b8c5d7987bf13950a1f08 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 12 Apr 2022 01:07:55 -0500 Subject: [PATCH] Fix 10955: False positive: containerOutOfBounds when using a const reference member (#4005) --- lib/valueflow.cpp | 2 +- test/teststl.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 1bfdad0a4..d070bab94 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7733,7 +7733,7 @@ static void valueFlowContainerSize(TokenList* tokenlist, if (size < 0) continue; } - if (!staticSize && !var->isConst() && nonLocal) + if (!staticSize && nonLocal) continue; if (var->nameToken()->hasKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE)) continue; diff --git a/test/teststl.cpp b/test/teststl.cpp index 33813a374..85efdf3f2 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -824,6 +824,26 @@ private: "}\n"); ASSERT_EQUALS("test.cpp:3:error:Out of bounds access in 'array[i]', if 'array' size is 10 and 'i' is 10\n", errout.str()); + + checkNormal("struct A {\n" + " const std::vector& v;\n" + " A(const std::vector& x) : v(x)\n" + " {}\n" + " int f() const {\n" + " return v[0];\n" + " }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + + checkNormal("struct A {\n" + " static const std::vector v;\n" + " int f() const {\n" + " return v[0];\n" + " }\n" + "};\n" + "const std::vector A::v = {1, 2};\n"); + ASSERT_EQUALS("", errout.str()); + } void outOfBoundsSymbolic()