Skip to content

Commit 2566fd0

Browse files
committed
Fixed cppcheck-opensource#5803 (False positive: Same iterator is used with different containers - insert() from range of different container)
1 parent 55b3f0b commit 2566fd0

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

lib/checkstl.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ void CheckStl::iterators()
165165
if (itTok->previous()->str() == "*")
166166
continue;
167167

168+
// inserting iterator range..
169+
if (tok2->strAt(2) == "insert") {
170+
const Token *par2 = itTok->nextArgument();
171+
while (par2 && par2->str() != ")") {
172+
if (par2->varId() == container->declarationId())
173+
break;
174+
if (par2->str() == "(")
175+
par2 = par2->link();
176+
par2 = par2->next();
177+
}
178+
if (par2->varId() == container->declarationId())
179+
continue;
180+
}
181+
168182
// Show error message, mismatching iterator is used.
169183
iteratorsError(tok2, container->name(), tok2->str());
170184
}

test/teststl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ class TestStl : public TestFixture {
201201
" l2.insert(it, 0);\n"
202202
"}");
203203
ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
204+
205+
check("void foo() {\n" // #5803
206+
" list<int> l1;\n"
207+
" list<int> l2;\n"
208+
" list<int>::iterator it = l1.begin();\n"
209+
" l2.insert(it, l1.end());\n"
210+
"}");
211+
ASSERT_EQUALS("", errout.str());
204212
}
205213

206214
void iterator4() {

0 commit comments

Comments
 (0)