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()