From e247e818ecbaed85753a31fd67ecd44915132518 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 11 Oct 2023 14:08:17 +0200 Subject: [PATCH] Fix #12052 FP: containerOutOfBounds (#5534) --- lib/checkstl.cpp | 4 ++-- lib/valueflow.cpp | 4 ++-- test/fixture.h | 2 +- test/testvalueflow.cpp | 21 +++++++++++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 489899868..498682e76 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1930,8 +1930,8 @@ void CheckStl::string_c_str() // Find all functions that take std::string as argument struct StrArg { - nonneg int n; // cppcheck-suppress unusedStructMember // FP used through iterator/pair - std::string argtype; // cppcheck-suppress unusedStructMember + nonneg int n; + std::string argtype; }; std::multimap c_strFuncParam; if (printPerformance) { diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c71e93ee6..ecd1bed5c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -8526,10 +8526,10 @@ static std::vector getContainerSizeFromConstructorArgs(const s static bool valueFlowIsSameContainerType(const ValueType& contType, const Token* tok, const Settings* settings) { if (!tok || !tok->valueType() || !tok->valueType()->containerTypeToken) - return false; + return true; const ValueType tokType = ValueType::parseDecl(tok->valueType()->containerTypeToken, *settings); - return contType.isTypeEqual(&tokType); + return contType.isTypeEqual(&tokType) || tokType.type == ValueType::Type::UNKNOWN_TYPE; } static std::vector getInitListSize(const Token* tok, diff --git a/test/fixture.h b/test/fixture.h index ac3e9fd97..4e5f7262d 100644 --- a/test/fixture.h +++ b/test/fixture.h @@ -118,7 +118,7 @@ protected: static T& getCheck() { for (Check *check : Check::instances()) { - //cppcheck-suppress [constVariablePointer, useStlAlgorithm] - TODO: fix constVariable FP + //cppcheck-suppress useStlAlgorithm if (T* c = dynamic_cast(check)) return *c; } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index a316ea101..9e0937b2a 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -6670,6 +6670,27 @@ private: "}\n"; ASSERT(!isKnownContainerSizeValue(tokenValues(code, "v ["), 0).empty()); ASSERT(!isPossibleContainerSizeValue(tokenValues(code, "v ["), 0).empty()); + + code = "template\n" // #12052 + "std::vector g(T);\n" + "int f() {\n" + " const std::vector v{ g(3) };\n" + " auto x = v.size();\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfXKnown(code, 6U, 1)); + + code = "template\n" + "std::vector g(const std::stack& s);\n" + "int f() {\n" + " std::stack s{};\n" + " s.push(42);\n" + " s.push(43);\n" + " const std::vector r{ g(s) };\n" + " auto x = r.size();\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 1)); } void valueFlowContainerElement()