Fix 10955: False positive: containerOutOfBounds when using a const reference member (#4005)

This commit is contained in:
Paul Fultz II 2022-04-12 01:07:55 -05:00 committed by GitHub
parent 00badff622
commit 61b87bcc60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -7733,7 +7733,7 @@ static void valueFlowContainerSize(TokenList* tokenlist,
if (size < 0) if (size < 0)
continue; continue;
} }
if (!staticSize && !var->isConst() && nonLocal) if (!staticSize && nonLocal)
continue; continue;
if (var->nameToken()->hasKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE)) if (var->nameToken()->hasKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE))
continue; continue;

View File

@ -824,6 +824,26 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("test.cpp:3:error:Out of bounds access in 'array[i]', if 'array' size is 10 and 'i' is 10\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()); errout.str());
checkNormal("struct A {\n"
" const std::vector<int>& v;\n"
" A(const std::vector<int>& 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<int> v;\n"
" int f() const {\n"
" return v[0];\n"
" }\n"
"};\n"
"const std::vector<int> A::v = {1, 2};\n");
ASSERT_EQUALS("", errout.str());
} }
void outOfBoundsSymbolic() void outOfBoundsSymbolic()