Skip to content

Commit e247e81

Browse files
Fix #12052 FP: containerOutOfBounds (#5534)
1 parent 784b526 commit e247e81

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

lib/checkstl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,8 +1930,8 @@ void CheckStl::string_c_str()
19301930

19311931
// Find all functions that take std::string as argument
19321932
struct StrArg {
1933-
nonneg int n; // cppcheck-suppress unusedStructMember // FP used through iterator/pair
1934-
std::string argtype; // cppcheck-suppress unusedStructMember
1933+
nonneg int n;
1934+
std::string argtype;
19351935
};
19361936
std::multimap<const Function*, StrArg> c_strFuncParam;
19371937
if (printPerformance) {

lib/valueflow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8526,10 +8526,10 @@ static std::vector<ValueFlow::Value> getContainerSizeFromConstructorArgs(const s
85268526
static bool valueFlowIsSameContainerType(const ValueType& contType, const Token* tok, const Settings* settings)
85278527
{
85288528
if (!tok || !tok->valueType() || !tok->valueType()->containerTypeToken)
8529-
return false;
8529+
return true;
85308530

85318531
const ValueType tokType = ValueType::parseDecl(tok->valueType()->containerTypeToken, *settings);
8532-
return contType.isTypeEqual(&tokType);
8532+
return contType.isTypeEqual(&tokType) || tokType.type == ValueType::Type::UNKNOWN_TYPE;
85338533
}
85348534

85358535
static std::vector<ValueFlow::Value> getInitListSize(const Token* tok,

test/fixture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class TestFixture : public ErrorLogger {
118118
static T& getCheck()
119119
{
120120
for (Check *check : Check::instances()) {
121-
//cppcheck-suppress [constVariablePointer, useStlAlgorithm] - TODO: fix constVariable FP
121+
//cppcheck-suppress useStlAlgorithm
122122
if (T* c = dynamic_cast<T*>(check))
123123
return *c;
124124
}

test/testvalueflow.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6670,6 +6670,27 @@ class TestValueFlow : public TestFixture {
66706670
"}\n";
66716671
ASSERT(!isKnownContainerSizeValue(tokenValues(code, "v ["), 0).empty());
66726672
ASSERT(!isPossibleContainerSizeValue(tokenValues(code, "v ["), 0).empty());
6673+
6674+
code = "template<typename T>\n" // #12052
6675+
"std::vector<T> g(T);\n"
6676+
"int f() {\n"
6677+
" const std::vector<int> v{ g(3) };\n"
6678+
" auto x = v.size();\n"
6679+
" return x;\n"
6680+
"}\n";
6681+
ASSERT_EQUALS(false, testValueOfXKnown(code, 6U, 1));
6682+
6683+
code = "template<typename T>\n"
6684+
"std::vector<T> g(const std::stack<T>& s);\n"
6685+
"int f() {\n"
6686+
" std::stack<int> s{};\n"
6687+
" s.push(42);\n"
6688+
" s.push(43);\n"
6689+
" const std::vector<int> r{ g(s) };\n"
6690+
" auto x = r.size();\n"
6691+
" return x;\n"
6692+
"}\n";
6693+
ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 1));
66736694
}
66746695

66756696
void valueFlowContainerElement()

0 commit comments

Comments
 (0)