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
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<const Function*, StrArg> c_strFuncParam;
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)
{
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<ValueFlow::Value> getInitListSize(const Token* tok,

View File

@ -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<T*>(check))
return *c;
}

View File

@ -6670,6 +6670,27 @@ private:
"}\n";
ASSERT(!isKnownContainerSizeValue(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()