Skip to content

Commit

Permalink
Fix #12052 FP: containerOutOfBounds (danmar#5534)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Oct 11, 2023
1 parent 784b526 commit e247e81
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class TestFixture : public ErrorLogger {
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;
}
Expand Down
21 changes: 21 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6670,6 +6670,27 @@ class TestValueFlow : public TestFixture {
"}\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()
Expand Down

0 comments on commit e247e81

Please sign in to comment.