Fix #12052 FP: containerOutOfBounds (#5534)

This commit is contained in:
chrchr-github 2023-10-11 14:08:17 +02:00 committed by GitHub
parent 784b526365
commit e247e818ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 5 deletions

View File

@ -1930,8 +1930,8 @@ void CheckStl::string_c_str()
// Find all functions that take std::string as argument // Find all functions that take std::string as argument
struct StrArg { struct StrArg {
nonneg int n; // cppcheck-suppress unusedStructMember // FP used through iterator/pair nonneg int n;
std::string argtype; // cppcheck-suppress unusedStructMember std::string argtype;
}; };
std::multimap<const Function*, StrArg> c_strFuncParam; std::multimap<const Function*, StrArg> c_strFuncParam;
if (printPerformance) { if (printPerformance) {

View File

@ -8526,10 +8526,10 @@ static std::vector<ValueFlow::Value> getContainerSizeFromConstructorArgs(const s
static bool valueFlowIsSameContainerType(const ValueType& contType, const Token* tok, const Settings* settings) static bool valueFlowIsSameContainerType(const ValueType& contType, const Token* tok, const Settings* settings)
{ {
if (!tok || !tok->valueType() || !tok->valueType()->containerTypeToken) if (!tok || !tok->valueType() || !tok->valueType()->containerTypeToken)
return false; return true;
const ValueType tokType = ValueType::parseDecl(tok->valueType()->containerTypeToken, *settings); 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<ValueFlow::Value> getInitListSize(const Token* tok, static std::vector<ValueFlow::Value> getInitListSize(const Token* tok,

View File

@ -118,7 +118,7 @@ protected:
static T& getCheck() static T& getCheck()
{ {
for (Check *check : Check::instances()) { for (Check *check : Check::instances()) {
//cppcheck-suppress [constVariablePointer, useStlAlgorithm] - TODO: fix constVariable FP //cppcheck-suppress useStlAlgorithm
if (T* c = dynamic_cast<T*>(check)) if (T* c = dynamic_cast<T*>(check))
return *c; return *c;
} }

View File

@ -6670,6 +6670,27 @@ private:
"}\n"; "}\n";
ASSERT(!isKnownContainerSizeValue(tokenValues(code, "v ["), 0).empty()); ASSERT(!isKnownContainerSizeValue(tokenValues(code, "v ["), 0).empty());
ASSERT(!isPossibleContainerSizeValue(tokenValues(code, "v ["), 0).empty()); ASSERT(!isPossibleContainerSizeValue(tokenValues(code, "v ["), 0).empty());
code = "template<typename T>\n" // #12052
"std::vector<T> g(T);\n"
"int f() {\n"
" const std::vector<int> v{ g(3) };\n"
" auto x = v.size();\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXKnown(code, 6U, 1));
code = "template<typename T>\n"
"std::vector<T> g(const std::stack<T>& s);\n"
"int f() {\n"
" std::stack<int> s{};\n"
" s.push(42);\n"
" s.push(43);\n"
" const std::vector<int> r{ g(s) };\n"
" auto x = r.size();\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 1));
} }
void valueFlowContainerElement() void valueFlowContainerElement()