/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2018 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "checkstl.h"
#include "settings.h"
#include "standards.h"
#include "testsuite.h"
#include "tokenize.h"
#include
#include
class TestStl : public TestFixture {
public:
TestStl() : TestFixture("TestStl") {
}
private:
Settings settings;
void run() override {
settings.addEnabled("warning");
settings.addEnabled("style");
settings.addEnabled("performance");
LOAD_LIB_2(settings.library, "std.cfg");
TEST_CASE(outOfBounds);
TEST_CASE(iterator1);
TEST_CASE(iterator2);
TEST_CASE(iterator3);
TEST_CASE(iterator4);
TEST_CASE(iterator5);
TEST_CASE(iterator6);
TEST_CASE(iterator7);
TEST_CASE(iterator8);
TEST_CASE(iterator9);
TEST_CASE(iterator10);
TEST_CASE(iterator11);
TEST_CASE(iterator12);
TEST_CASE(iterator13);
TEST_CASE(iterator14); // #8191
TEST_CASE(iterator15); // #8341
TEST_CASE(iteratorExpression);
TEST_CASE(iteratorSameExpression);
TEST_CASE(dereference);
TEST_CASE(dereference_break); // #3644 - handle "break"
TEST_CASE(dereference_member);
TEST_CASE(STLSize);
TEST_CASE(STLSizeNoErr);
TEST_CASE(negativeIndex);
TEST_CASE(erase1);
TEST_CASE(erase2);
TEST_CASE(erase3);
TEST_CASE(erase4);
TEST_CASE(erase5);
TEST_CASE(erase6);
TEST_CASE(eraseBreak);
TEST_CASE(eraseContinue);
TEST_CASE(eraseReturn1);
TEST_CASE(eraseReturn2);
TEST_CASE(eraseReturn3);
TEST_CASE(eraseGoto);
TEST_CASE(eraseAssign1);
TEST_CASE(eraseAssign2);
TEST_CASE(eraseAssign3);
TEST_CASE(eraseAssign4);
TEST_CASE(eraseAssignByFunctionCall);
TEST_CASE(eraseErase);
TEST_CASE(eraseByValue);
TEST_CASE(eraseIf);
TEST_CASE(eraseOnVector);
TEST_CASE(pushback1);
TEST_CASE(pushback2);
TEST_CASE(pushback3);
TEST_CASE(pushback4);
TEST_CASE(pushback5);
TEST_CASE(pushback6);
TEST_CASE(pushback7);
TEST_CASE(pushback8);
TEST_CASE(pushback9);
TEST_CASE(pushback10);
TEST_CASE(pushback11);
TEST_CASE(pushback12);
TEST_CASE(pushback13);
TEST_CASE(insert1);
TEST_CASE(insert2);
TEST_CASE(stlBoundaries1);
TEST_CASE(stlBoundaries2);
TEST_CASE(stlBoundaries3);
TEST_CASE(stlBoundaries4); // #4364
TEST_CASE(stlBoundaries5); // #4352
TEST_CASE(stlBoundaries6); // #7106
// if (str.find("ab"))
TEST_CASE(if_find);
TEST_CASE(if_str_find);
TEST_CASE(size1);
TEST_CASE(size2);
TEST_CASE(size3);
TEST_CASE(size4); // #2652 - don't warn about vector/deque
// Redundant conditions..
// if (ints.find(123) != ints.end()) ints.remove(123);
TEST_CASE(redundantCondition1);
// missing inner comparison when incrementing iterator inside loop
TEST_CASE(missingInnerComparison1);
TEST_CASE(missingInnerComparison2); // no FP when there is comparison
TEST_CASE(missingInnerComparison3); // no FP when there is iterator shadowing
TEST_CASE(missingInnerComparison4); // no FP when "break;" is used
TEST_CASE(missingInnerComparison5); // Ticket #2154 - FP
TEST_CASE(missingInnerComparison6); // #2643 - 'it=foo.insert(++it,0);'
// catch common problems when using the string::c_str() function
TEST_CASE(cstr);
TEST_CASE(autoPointer);
TEST_CASE(uselessCalls);
TEST_CASE(stabilityOfChecks); // #4684 cppcheck crash in template function call
TEST_CASE(dereferenceInvalidIterator);
TEST_CASE(dereferenceInvalidIterator2); // #6572
TEST_CASE(dereference_auto);
}
void check(const char code[], const bool inconclusive=false, const Standards::cppstd_t cppstandard=Standards::CPP11) {
// Clear the error buffer..
errout.str("");
settings.inconclusive = inconclusive;
settings.standards.cpp = cppstandard;
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// Check..
CheckStl checkStl(&tokenizer, &settings, this);
checkStl.runSimplifiedChecks(&tokenizer, &settings, this);
}
void check(const std::string &code, const bool inconclusive=false) {
check(code.c_str(), inconclusive);
}
void checkNormal(const char code[]) {
// Clear the error buffer..
errout.str("");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Check..
CheckStl checkStl(&tokenizer, &settings, this);
checkStl.runChecks(&tokenizer, &settings, this);
}
void outOfBounds() {
setMultiline();
checkNormal("void f(std::vector v) {\n"
" v.front();\n"
" if (v.empty()) {}\n"
"}\n");
ASSERT_EQUALS("test.cpp:2:warning:Accessing an item in container 'v'. Either the condition 'v.empty()' is redundant or 'v' can be empty.\n"
"test.cpp:3:note:condition 'v.empty()'\n"
"test.cpp:2:note:Access out of bounds\n", errout.str());
checkNormal("void f(std::vector v) {\n"
" if (v.size() == 3) {}\n"
" v[16] = 0;\n"
"}\n");
ASSERT_EQUALS("test.cpp:3:warning:Possible access out of bounds of container 'v'; size=3, index=16\n"
"test.cpp:2:note:condition 'v.size()==3'\n"
"test.cpp:3:note:Access out of bounds\n", errout.str());
checkNormal("void f(std::vector v) {\n"
" int i = 16;\n"
" if (v.size() == 3) {\n"
" v[i] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("test.cpp:4:warning:Possible access out of bounds of container 'v'; size=3, index=16\n"
"test.cpp:3:note:condition 'v.size()==3'\n"
"test.cpp:4:note:Access out of bounds\n", errout.str());
checkNormal("void f(std::vector v, int i) {\n"
" if (v.size() == 3 || i == 16) {}\n"
" v[i] = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkNormal("void f(std::map x) {\n"
" if (x.empty()) { x[1] = 2; }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkNormal("void f(std::string s) {\n"
" if (s.size() == 1) {\n"
" s[2] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("test.cpp:3:warning:Possible access out of bounds of container 's'; size=1, index=2\n"
"test.cpp:2:note:condition 's.size()==1'\n"
"test.cpp:3:note:Access out of bounds\n", errout.str());
}
void iterator1() {
check("void f()\n"
"{\n"
" list l1;\n"
" list l2;\n"
" for (list::iterator it = l1.begin(); it != l2.end(); ++it)\n"
" { }\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
check("void f()\n"
"{\n"
" list l1;\n"
" list l2;\n"
" for (list::iterator it = l1.begin(); l2.end() != it; ++it)\n"
" { }\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
check("struct C { std::list l1; void func(); };\n"
"void C::func() {\n"
" std::list::iterator it;\n"
" for (it = l1.begin(); it != l1.end(); ++it) { }\n"
" C c;\n"
" for (it = c.l1.begin(); it != c.l1.end(); ++it) { }\n"
"}");
ASSERT_EQUALS("", errout.str());
// Same check with reverse iterator
check("void f()\n"
"{\n"
" list l1;\n"
" list l2;\n"
" for (list::const_reverse_iterator it = l1.rbegin(); it != l2.rend(); ++it)\n"
" { }\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
}
void iterator2() {
check("void foo()\n"
"{\n"
" list l1;\n"
" list l2;\n"
" list::iterator it = l1.begin();\n"
" while (it != l2.end())\n"
" {\n"
" ++it;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
}
void iterator3() {
check("void foo()\n"
"{\n"
" list l1;\n"
" list l2;\n"
" list::iterator it = l1.begin();\n"
" l2.insert(it, 0);\n"
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str());
check("void foo() {\n" // #5803
" list l1;\n"
" list l2;\n"
" list::iterator it = l1.begin();\n"
" l2.insert(it, l1.end());\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo() {\n" // #7658
" list l1;\n"
" list l2;\n"
" list::iterator it = l1.begin();\n"
" list::iterator end = l1.end();\n"
" l2.insert(it, end);\n"
"}");
ASSERT_EQUALS("", errout.str());
// only warn for insert when there are preciself 2 arguments.
check("void foo() {\n"
" list l1;\n"
" list l2;\n"
" list::iterator it = l1.begin();\n"
" l2.insert(it);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo() {\n"
" list l1;\n"
" list l2;\n"
" list::iterator it = l1.begin();\n"
" l2.insert(it,0,1);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void iterator4() {
check("void foo(std::vector &test)\n"
"{\n"
" std::set result;\n"
" for (std::vector::const_iterator cit = test.begin();\n"
" cit != test.end();\n"
" ++cit)\n"
" {\n"
" result.insert(cit->size());\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void iterator5() {
check("void foo()\n"
"{\n"
" std::vector ints1;\n"
" std::vector ints2;\n"
" std::vector::iterator it = std::find(ints1.begin(), ints2.end(), 22);\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
}
void iterator6() {
// Ticket #1357
check("void foo(const std::set &ints1)\n"
"{\n"
" std::set ints2;\n"
" std::set::iterator it1 = ints1.begin();\n"
" std::set::iterator it2 = ints1.end();\n"
" ints2.insert(it1, it2);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo(const std::set &ints1)\n"
"{\n"
" std::set ints2;\n"
" std::set::iterator it1 = ints1.begin();\n"
" std::set::iterator it2 = ints2.end();\n"
" ints2.insert(it1, it2);\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Iterators of different containers are used together.\n", "", errout.str());
}
void iterator7() {
check("void foo()\n"
"{\n"
" std::vector ints1;\n"
" std::vector ints2;\n"
" std::vector::iterator it = std::inplace_merge(ints1.begin(), std::advance(ints1.rbegin(), 5), ints2.end());\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
check("void foo()\n"
"{\n"
" std::vector ints1;\n"
" std::vector ints2;\n"
" std::vector::iterator it = std::inplace_merge(ints1.begin(), std::advance(ints2.rbegin(), 5), ints1.end());\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void iterator8() {
check("void foo()\n"
"{\n"
" std::vector ints1;\n"
" std::vector ints2;\n"
" std::vector::iterator it = std::find_first_of(ints1.begin(), ints2.end(), ints1.begin(), ints1.end());\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
check("void foo()\n"
"{\n"
" std::vector ints1;\n"
" std::vector ints2;\n"
" std::vector::iterator it = std::find_first_of(ints1.begin(), ints1.end(), ints2.begin(), ints1.end());\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
check("void foo()\n"
"{\n"
" std::vector ints1;\n"
" std::vector ints2;\n"
" std::vector::iterator it = std::find_first_of(foo.bar.begin(), foo.bar.end()-6, ints2.begin(), ints1.end());\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
check("void foo()\n"
"{\n"
" std::vector ints1;\n"
" std::vector ints2;\n"
" std::vector::iterator it = std::find_first_of(ints1.begin(), ints1.end(), ints2.begin(), ints2.end());\n"
"}");
ASSERT_EQUALS("", errout.str());
// #6839
check("void f(const std::wstring& a, const std::wstring& b) {\n"
" const std::string tp1 = std::string(a.begin(), b.end());\n"
" const std::wstring tp2 = std::string(b.begin(), a.end());\n"
" const std::u16string tp3(a.begin(), b.end());\n"
" const std::u32string tp4(b.begin(), a.end());\n"
" const std::string fp1 = std::string(a.begin(), a.end());\n"
" const std::string tp2(a.begin(), a.end());\n"
"}");
ASSERT_EQUALS(// TODO "[test.cpp:2]: (error) Iterators of different containers are used together.\n"
// TODO "[test.cpp:3]: (error) Iterators of different containers are used together.\n"
"[test.cpp:4]: (error) Iterators of different containers are used together.\n"
"[test.cpp:5]: (error) Iterators of different containers are used together.\n", errout.str());
}
void iterator9() {
// Ticket #1600
check("void foo(std::vector &r)\n"
"{\n"
" std::vector::iterator aI = r.begin();\n"
" while(aI != r.end())\n"
" {\n"
" if (*aI == 0)\n"
" {\n"
" r.insert(aI, 42);\n"
" return;\n"
" }\n"
" ++aI;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
// Ticket #2481
check("void foo(std::vector &r)\n"
"{\n"
" std::vector::iterator aI = r.begin();\n"
" while(aI != r.end())\n"
" {\n"
" if (*aI == 0)\n"
" {\n"
" r.insert(aI, 42);\n"
" break;\n"
" }\n"
" ++aI;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
// Execution path checking..
check("void foo(std::vector &r, int c)\n"
"{\n"
" std::vector::iterator aI = r.begin();\n"
" while(aI != r.end())\n"
" {\n"
" if (*aI == 0)\n"
" {\n"
" r.insert(aI, 42);\n"
" if (c)\n"
" {\n"
" return;\n"
" }\n"
" }\n"
" ++aI;\n"
" }\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:14] (error) After insert(), the iterator 'aI' may be invalid.", "", errout.str());
}
void iterator10() {
// Ticket #1679
check("void foo()\n"
"{\n"
" std::set s1;\n"
" std::set s2;\n"
" for (std::set::iterator it = s1.begin(); it != s1.end(); ++it)\n"
" {\n"
" if (true) { }\n"
" if (it != s2.end()) continue;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:8]: (error) Same iterator is used with different containers 's1' and 's2'.\n", errout.str());
}
void iterator11() {
// Ticket #3433
check("int main() {\n"
" map myMap;\n"
" vector myVector;\n"
" for(vector::iterator x = myVector.begin(); x != myVector.end(); x++)\n"
" myMap.erase(*x);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void iterator12() {
// Ticket #3201
check("void f() {\n"
" std::map map1;\n"
" std::map map2;\n"
" std::map::const_iterator it = map1.find(123);\n"
" if (it == map2.end()) { }"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with different containers 'map1' and 'map2'.\n", errout.str());
check("void f() {\n"
" std::map map1;\n"
" std::map map2;\n"
" std::map::const_iterator it = map1.find(123);\n"
" if (map2.end() == it) { }"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with different containers 'map1' and 'map2'.\n", errout.str());
check("void f(std::string &s) {\n"
" int pos = s.find(x);\n"
" s.erase(pos);\n"
" s.erase(pos);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void iterator13() {
check("void f() {\n"
" std::vector a;\n"
" std::vector t;\n"
" std::vector::const_iterator it;\n"
" it = a.begin();\n"
" while (it!=a.end())\n"
" ++it;\n"
" it = t.begin();\n"
" while (it!=a.end())\n"
" ++it;\n"
"}");
ASSERT_EQUALS("[test.cpp:9]: (error) Same iterator is used with different containers 't' and 'a'.\n", errout.str());
// #4062
check("void f() {\n"
" std::vector a;\n"
" std::vector t;\n"
" std::vector::const_iterator it;\n"
" it = a.begin();\n"
" while (it!=a.end())\n"
" v++it;\n"
" it = t.begin();\n"
" while (it!=t.end())\n"
" ++it;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" std::vector a;\n"
" std::vector t;\n"
" std::vector::const_iterator it;\n"
" if(z)\n"
" it = a.begin();\n"
" else\n"
" it = t.begin();\n"
" while (z && it!=a.end())\n"
" v++it;\n"
" while (!z && it!=t.end())\n"
" v++it;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void iterator14() {
check("void f() {\n"
" std::map x;\n"
" std::map::const_iterator it;\n"
" for (it = x.find(0)->second.begin(); it != x.find(0)->second.end(); ++it) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void iterator15() {
check("void f(C1* x, std::list a) {\n"
" std::list::iterator pos = a.begin();\n"
" for(pos = x[0]->plist.begin(); pos != x[0]->plist.end(); ++pos) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void iteratorExpression() {
check("std::vector& f();\n"
"std::vector& g();\n"
"void foo() {\n"
" (void)std::find(f().begin(), g().end(), 0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Iterators to containers from different expressions 'f()' and 'g()' are used together.\n", errout.str());
check("std::vector& f();\n"
"std::vector& g();\n"
"void foo() {\n"
" if(f().begin() == g().end()) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Iterators to containers from different expressions 'f()' and 'g()' are used together.\n", errout.str());
check("std::vector& f();\n"
"std::vector& g();\n"
"void foo() {\n"
" auto size = f().end() - g().begin();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Iterators to containers from different expressions 'f()' and 'g()' are used together.\n", errout.str());
check("struct A {\n"
" std::vector& f();\n"
" std::vector& g();\n"
"};\n"
"void foo() {\n"
" (void)std::find(A().f().begin(), A().g().end(), 0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (warning) Iterators to containers from different expressions 'A().f()' and 'A().g()' are used together.\n", errout.str());
check("struct A {\n"
" std::vector& f();\n"
" std::vector& g();\n"
"};\n"
"void foo() {\n"
" (void)std::find(A{}.f().begin(), A{}.g().end(), 0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (warning) Iterators to containers from different expressions 'A{}.f()' and 'A{}.g()' are used together.\n", errout.str());
check("std::vector& f();\n"
"std::vector& g();\n"
"void foo() {\n"
" (void)std::find(begin(f()), end(g()), 0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Iterators to containers from different expressions 'f()' and 'g()' are used together.\n", errout.str());
check("struct A {\n"
" std::vector& f();\n"
" std::vector& g();\n"
"};\n"
"void foo() {\n"
" (void)std::find(A().f().begin(), A().f().end(), 0);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("std::vector& f();\n"
"std::vector& g();\n"
"void foo() {\n"
" if(bar(f().begin()) == g().end()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("std::vector& f();\n"
"std::vector& g();\n"
"void foo() {\n"
" auto it = f().end();\n"
" f().begin() - it\n"
" f().begin()+1 - it\n"
" f().begin() - (it + 1)\n"
" f().begin() - f().end()\n"
" f().begin()+1 - f().end()\n"
" f().begin() - (f().end() + 1)\n"
" (void)std::find(f().begin(), it, 0);\n"
" (void)std::find(f().begin(), it + 1, 0);\n"
" (void)std::find(f().begin() + 1, it + 1, 0);\n"
" (void)std::find(f().begin() + 1, it, 0);\n"
" (void)std::find(f().begin(), f().end(), 0);\n"
" (void)std::find(f().begin() + 1, f().end(), 0);\n"
" (void)std::find(f().begin(), f().end() - 1, 0);\n"
" (void)std::find(f().begin() + 1, f().end() - 1, 0);\n"
" (void)std::find(begin(f()), end(f()));\n"
" (void)std::find(begin(f()) + 1, end(f()), 0);\n"
" (void)std::find(begin(f()), end(f()) - 1, 0);\n"
" (void)std::find(begin(f()) + 1, end(f()) - 1, 0);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("std::vector& f();\n"
"std::vector& g();\n"
"void foo() {\n"
" if(f().begin() == f().end()) {}\n"
" if(f().begin() == f().end()+1) {}\n"
" if(f().begin()+1 == f().end()) {}\n"
" if(f().begin()+1 == f().end()+1) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" if (a.begin().x == b.begin().x) {}\n"
" if (begin(a).x == begin(b).x) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" std::list a;\n"
" std::list b;\n"
" if (*a.begin() == *b.begin()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void iteratorSameExpression() {
check("void f(std::vector v) {\n"
" std::for_each(v.begin(), v.begin(), [](int){});\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("std::vector& g();\n"
"void f() {\n"
" std::for_each(g().begin(), g().begin(), [](int){});\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("void f(std::vector v) {\n"
" std::for_each(v.end(), v.end(), [](int){});\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("std::vector& g();\n"
"void f() {\n"
" std::for_each(g().end(), g().end(), [](int){});\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("std::vector::iterator g();\n"
"void f(std::vector v) {\n"
" std::for_each(g(), g(), [](int){});\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("void f(std::vector::iterator it) {\n"
" std::for_each(it, it, [](int){});\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Same iterators expression are used for algorithm.\n", errout.str());
}
// Dereferencing invalid pointer
void dereference() {
check("void f()\n"
"{\n"
" std::vector ints;\n"
" std::vector::iterator iter;\n"
" iter = ints.begin() + 2;\n"
" ints.erase(iter);\n"
" std::cout << (*iter) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
// #6554 "False positive eraseDereference - erase in while() loop"
check("typedef std::map packetMap;\n"
"packetMap waitingPackets;\n"
"void ProcessRawPacket() {\n"
" packetMap::iterator wpi;\n"
" while ((wpi = waitingPackets.find(lastInOrder + 1)) != waitingPackets.end()) {\n"
" waitingPackets.erase(wpi);\n"
" for (unsigned pos = 0; pos < buf.size(); ) { }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
// #8509 Uniform initialization ignored for iterator
check("void f() {\n"
" std::vector ints;\n"
" std::vector::const_iterator iter {ints.cbegin()};\n"
" std::cout << (*iter) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void dereference_break() { // #3644
check("void f(std::vector &ints) {\n"
" std::vector::iterator iter;\n"
" for (iter=ints.begin();iter!=ints.end();++iter) {\n"
" if (*iter == 2) {\n"
" ints.erase(iter);\n"
" break;\n"
" }\n"
" if (*iter == 3) {\n"
" ints.erase(iter);\n"
" break;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void dereference_member() {
check("void f()\n"
"{\n"
" std::map ints;\n"
" std::map::iterator iter;\n"
" iter = ints.begin();\n"
" ints.erase(iter);\n"
" std::cout << iter->first << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
// Reverse iterator
check("void f()\n"
"{\n"
" std::map ints;\n"
" std::map::reverse_iterator iter;\n"
" iter = ints.rbegin();\n"
" ints.erase(iter);\n"
" std::cout << iter->first << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
}
void dereference_auto() {
check("void f()\n"
"{\n"
" std::vector ints;\n"
" auto iter = ints.begin() + 2;\n"
" ints.erase(iter);\n"
" std::cout << (*iter) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:5]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
check("void f() {\n"
" auto x = *myList.begin();\n"
" myList.erase(x);\n"
" auto b = x.first;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {\n"
" if (auto *TD = dyn_cast(this)) {\n"
" auto From = TD->getInstantiatedFrom();\n"
" }\n"
" return nullptr;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void STLSize() {
check("void foo()\n"
"{\n"
" std::vector foo;\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" foo[ii] = 0;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) When ii==foo.size(), foo[ii] is out of bounds.\n", errout.str());
check("void foo(std::vector foo) {\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii) {\n"
" foo.at(ii) = 0;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) When ii==foo.size(), foo.at(ii) is out of bounds.\n", errout.str());
check("void foo(const std::string& foo) {\n"
" for (unsigned int ii = 0; ii <= foo.length(); ++ii) {\n"
" foo[ii] = 'x';\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) When ii==foo.size(), foo[ii] is out of bounds.\n", errout.str());
check("void foo(const std::string& foo, unsigned int ii) {\n"
" if (ii <= foo.length()) {\n"
" foo[ii] = 'x';\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) When ii==foo.size(), foo[ii] is out of bounds.\n", errout.str());
check("void foo(const std::string& foo, unsigned int ii) {\n"
" do {\n"
" foo[ii] = 'x';\n"
" ++i;\n"
" } while(ii <= foo.length());\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) When ii==foo.size(), foo[ii] is out of bounds.\n", errout.str());
check("void foo(const std::string& foo, unsigned int ii) {\n"
" if (anything()) {\n"
" } else if (ii <= foo.length()) {\n"
" foo[ii] = 'x';\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) When ii==foo.size(), foo[ii] is out of bounds.\n", errout.str());
check("void foo()\n"
"{\n"
" std::vector foo;\n"
" foo.push_back(1);\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" }\n"
" int ii = 0;\n"
" foo[ii] = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo() {\n"
" for (B b : D()) {}\n" // Don't crash on range-based for-loop
"}");
ASSERT_EQUALS("", errout.str());
}
void STLSizeNoErr() {
{
check("void foo()\n"
"{\n"
" std::vector foo;\n"
" for (unsigned int ii = 0; ii < foo.size(); ++ii)\n"
" {\n"
" foo[ii] = 0;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
{
check("void foo()\n"
"{\n"
" std::vector foo;\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
{
check("void foo()\n"
"{\n"
" std::vector foo;\n"
" for (unsigned int ii = 0; ii <= foo.size(); ++ii)\n"
" {\n"
" if (ii == foo.size())\n"
" {\n"
" }\n"
" else\n"
" {\n"
" foo[ii] = 0;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
{
check("void f(const std::map &data) {\n"
" int i = x;"
" for (int i = 5; i <= data.size(); i++)\n"
" data[i] = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
}
void negativeIndex() {
check("void f(const std::vector &v) {\n"
" v[-11] = 123;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Array index -11 is out of bounds.\n", errout.str());
}
void erase1() {
check("void f()\n"
"{\n"
" std::list::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it) {\n"
" foo.erase(it);\n"
" }\n"
" for (it = foo.begin(); it != foo.end(); ++it) {\n"
" foo.erase(it);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (error) Iterator 'it' used after element has been erased.\n"
"[test.cpp:7] -> [test.cpp:8]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
check("void f(std::list &ints)\n"
"{\n"
" std::list::iterator i = ints.begin();\n"
" i = ints.erase(i);\n"
" *i = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" std::list::iterator i;\n"
" while (i != x.y.end())\n"
" i = x.y.erase(i);\n"
"}");
ASSERT_EQUALS("", errout.str());
// #2101
check("void f(vector< list > &ints, unsigned int i)\n"
"{\n"
" list::iterator it;\n"
" for(it = ints[i].begin(); it != ints[i].end(); it++) {\n"
" if (*it % 2)\n"
" it = ints[i].erase(it);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void erase2() {
check("static void f()\n"
"{\n"
" for (iterator it = foo.begin(); it != foo.end(); it = next)\n"
" {\n"
" next = it;\n"
" next++;\n"
" foo.erase(it);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void erase3() {
check("static void f(std::list &foo)\n"
"{\n"
" std::list::iterator it = foo.begin();\n"
" foo.erase(it->a);\n"
" if (it->b);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void erase4() {
check("void f()\n"
"{\n"
" std::list::iterator it, it2;\n"
" for (it = foo.begin(); it != i2; ++it)\n"
" {\n"
" foo.erase(it);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
check("void f()\n"
"{\n"
" std::list::iterator it = foo.begin();\n"
" for (; it != i2; ++it)\n"
" {\n"
" foo.erase(it);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
check("void f()\n"
"{\n"
" std::list::iterator it = foo.begin();\n"
" while (it != i2)\n"
" {\n"
" foo.erase(it);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
check("void f()\n"
"{\n"
" std::list::iterator it = foo.begin();\n"
" while (it != i2)\n"
" {\n"
" foo.erase(++it);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
}
void erase5() {
check("void f()\n"
"{\n"
" std::list foo;\n"
" std::list::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" if (*it == 123)\n"
" foo.erase(it);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:8]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
}
void erase6() {
check("void f() {\n"
" std::vector vec(3);\n"
" std::vector::iterator it;\n"
" std::vector::iterator itEnd = vec.end();\n"
" for (it = vec.begin(); it != itEnd; it = vec.begin(), itEnd = vec.end())\n"
" {\n"
" vec.erase(it);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseBreak() {
check("void f()\n"
"{\n"
" for (std::vector::iterator it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" if (x)"
" break;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
check("void f()\n"
"{\n"
" for (std::vector::iterator it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" if (x) {\n"
" foo.erase(it);\n"
" break;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x)\n"
"{\n"
" for (std::vector::iterator it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" if (x)"
" return;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
}
void eraseContinue() {
check("void f(std::vector &ints)\n"
"{\n"
" std::vector::iterator it;\n"
" std::vector::iterator jt = ints.begin();\n"
" for (it = ints.begin(); it != ints.end(); it = jt) {\n"
" ++jt;\n"
" if (*it == 1) {\n"
" jt = ints.erase(it);\n"
" continue;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(std::map my_map) {\n" // #7365
" std::map::iterator itr = my_map.begin();\n"
" switch (itr->first) {\n"
" case 0:\n"
" my_map.erase(itr);\n"
" continue;\n"
" case 1:\n"
" itr->second = 1;\n"
" break;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseReturn1() {
check("void f()\n"
"{\n"
" std::vector foo;\n"
" std::vector::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" return;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseReturn2() {
check("void f()\n"
"{\n"
" std::vector foo;\n"
" std::vector::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" if (*it == 1) {\n"
" foo.erase(it);\n"
" return;\n"
" }\n"
" else {\n"
" foo.erase(it);\n"
" return;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseReturn3() {
check("void f()\n"
"{\n"
" std::vector foo;\n"
" std::vector::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" if (somecondition) {\n"
" if (*it == 1)\n"
" foo.erase(it);\n"
" else\n"
" *it = 0;\n"
" return;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" std::vector foo;\n"
" std::vector::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" if (a) {\n"
" if (b)\n"
" foo.erase(it);\n"
" else\n"
" *it = 0;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:9]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
}
void eraseGoto() {
check("void f()\n"
"{\n"
" for (std::vector::iterator it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" goto abc;\n"
" }\n"
"bar:\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseAssign1() {
check("void f()\n"
"{\n"
" for (std::vector::iterator it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
" it = foo.begin();\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseAssign2() {
check("void f(list &ints)\n"
"{\n"
" for (list::iterator it = ints.begin(); it != ints.end();) {\n"
" if (*it == 123) {\n"
" list::iterator copy = it;\n"
" ++copy;\n"
" ints.erase(it);\n"
" it = copy;\n"
" } else {\n"
" it->second = 123;\n"
" ++it;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseAssign3() {
check("void f(std::list >& l) {\n"
" std::list >::const_iterator i = l.begin();\n"
" std::list::const_iterator j = (*i).begin();\n"
" cout << *j << endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseAssign4() {
check("void f(std::list data) {\n"
" std::list::const_iterator it = data.begin();\n"
" it = data.erase(it);\n"
" it = data.erase(it);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(Data data) {\n"
" std::list::const_iterator it = data.ints.begin();\n"
" it = data.ints.erase(it);\n"
" it = data.ints.erase(it);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseAssignByFunctionCall() {
check("void f(std::list >& l) {\n"
" std::list::const_iterator i;\n"
" bar(i);\n"
" cout << *i;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseErase() {
check("void f(std::vector &ints)\n"
"{\n"
" std::vector::iterator iter;\n"
" iter = ints.begin() + 2;\n"
" ints.erase(iter);\n"
" ints.erase(iter);\n"
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) Invalid iterator: iter\n", errout.str());
}
void eraseByValue() {
check("void f()\n"
"{\n"
" std::set foo;\n"
" for (std::set::iterator it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(*it);\n"
" }\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Iterator 'it' becomes invalid when deleted by value from 'foo'\n", "", errout.str());
check("int f(std::set foo) {\n"
" std::set::iterator it = foo.begin();\n"
" foo.erase(*it);\n"
" return *it;\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (error) Iterator 'it' used after element has been erased.\n", errout.str());
check("void f()\n"
"{\n"
" std::set foo;\n"
" std::set::iterator it = foo.begin();\n"
" foo.erase(*it);\n"
"}");
ASSERT_EQUALS("", errout.str());
// #5669
check("void f() {\n"
" HashSet_Ref::iterator aIt = m_ImplementationMap.find( xEle );\n"
" m_SetLoadedFactories.erase(*aIt);\n"
" m_SetLoadedFactories.erase(aIt);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(const std::list& m_ImplementationMap) {\n"
" std::list::iterator aIt = m_ImplementationMap.find( xEle );\n"
" m_ImplementationMap.erase(*aIt);\n"
" m_ImplementationMap.erase(aIt);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Invalid iterator: aIt\n", errout.str());
check("void f(const std::list& m_ImplementationMap) {\n"
" std::list::iterator aIt = m_ImplementationMap.find( xEle1 );\n"
" std::list::iterator bIt = m_ImplementationMap.find( xEle2 );\n"
" m_ImplementationMap.erase(*bIt);\n"
" m_ImplementationMap.erase(aIt);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseIf() {
// #4816
check("void func(std::list strlist) {\n"
" for (std::list::iterator str = strlist.begin(); str != strlist.end(); str++) {\n"
" if (func2(*str)) {\n"
" strlist.erase(str);\n"
" if (strlist.empty())\n"
" return;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (error) Iterator 'str' used after element has been erased.\n", errout.str());
}
void eraseOnVector() {
check("void f(const std::vector& m_ImplementationMap) {\n"
" std::vector::iterator aIt = m_ImplementationMap.find( xEle );\n"
" m_ImplementationMap.erase(something(unknown));\n" // All iterators become invalidated when erasing from std::vector
" m_ImplementationMap.erase(aIt);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) After erase(), the iterator 'aIt' may be invalid.\n", errout.str());
check("void f(const std::vector& m_ImplementationMap) {\n"
" std::vector::iterator aIt = m_ImplementationMap.find( xEle );\n"
" m_ImplementationMap.erase(*aIt);\n" // All iterators become invalidated when erasing from std::vector
" m_ImplementationMap.erase(aIt);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Invalid iterator: aIt\n", errout.str());
check("void f(const std::vector& m_ImplementationMap) {\n"
" std::vector::iterator aIt = m_ImplementationMap.find( xEle1 );\n"
" std::vector::iterator bIt = m_ImplementationMap.find( xEle2 );\n"
" m_ImplementationMap.erase(*bIt);\n" // All iterators become invalidated when erasing from std::vector
" aIt = m_ImplementationMap.erase(aIt);\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) After erase(), the iterator 'aIt' may be invalid.\n", errout.str());
}
void pushback1() {
check("void f(const std::vector &foo)\n"
"{\n"
" std::vector::const_iterator it = foo.begin();\n"
" foo.push_back(123);\n"
" *it;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
}
void pushback2() {
check("void f()\n"
"{\n"
" std::vector::const_iterator it = foo.begin();\n"
" foo.push_back(123);\n"
" {\n"
" int *it = &foo[0];\n"
" *it = 456;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void pushback3() {
check("void f()\n"
"{\n"
" std::vector foo;\n"
" foo.push_back(10);\n"
" std::vector::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.push_back(123);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
}
void pushback4() {
check("void f()\n"
"{\n"
" std::vector ints;\n"
" ints.push_back(1);\n"
" int *first = &ints[0];\n"
" ints.push_back(2);\n"
" *first;\n"
"}");
ASSERT_EQUALS("[test.cpp:7]: (error) Invalid pointer 'first' after push_back().\n", errout.str());
}
void pushback5() {
check("void f()\n"
"{\n"
" std::vector::const_iterator i;\n"
"\n"
" for (i=v.begin(); i!=v.end(); ++i)\n"
" {\n"
" }\n"
"\n"
" for (i=rhs.v.begin(); i!=rhs.v.end(); ++i)\n"
" {\n"
" v.push_back(*i);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void pushback6() {
// ticket #735
check("void f()\n"
"{\n"
" vector v;\n"
" vector.push_back(1);\n"
" vector.push_back(2);\n"
" for (vector::iterator it = v.begin(); it != v.end(); ++it)\n"
" {\n"
" if (*it == 1)\n"
" v.push_back(10);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:9]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
check("void f()\n"
"{\n"
" std::vector v;\n"
" vector.push_back(1);\n"
" vector.push_back(2);\n"
" for (std::vector::iterator it = v.begin(); it != v.end(); ++it)\n"
" {\n"
" if (*it == 1)\n"
" v.push_back(10);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:9]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
}
void pushback7() {
check("void f()\n"
"{\n"
" std::vector foo;\n"
" foo.push_back(10);\n"
" std::vector::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); it++)\n"
" {\n"
" foo.push_back(123);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back(), the iterator 'it' may be invalid.\n", errout.str());
}
void pushback8() {
check("void f()\n"
"{\n"
" std::vector ints;\n"
" std::vector::const_iterator end = ints.end();\n"
" ints.push_back(10);\n"
" std::vector::iterator it;\n"
" unsigned int sum = 0;\n"
" for (it = ints.begin(); it != end; ++it)\n"
" {\n"
" sum += *it;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back(), the iterator 'end' may be invalid.\n", errout.str());
}
void pushback9() {
check("struct A {\n"
" std::vector ints;\n"
"};\n"
"\n"
"void f()\n"
"{\n"
" std::vector ints;\n"
" A a;\n"
" std::vector::const_iterator i = ints.begin();\n"
" std::vector::const_iterator e = ints.end();\n"
" while (i != e)\n"
" {\n"
" a.ints.push_back(*i);\n"
" ++i;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void pushback10() {
check("void f(std::vector &foo)\n"
"{\n"
" std::vector::const_iterator it = foo.begin();\n"
" foo.reserve(100);\n"
" *it = 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) After reserve(), the iterator 'it' may be invalid.\n", errout.str());
// in loop
check("void f()\n"
"{\n"
" std::vector foo;\n"
" foo.push_back(10);\n"
" std::vector::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.reserve(123);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:8]: (error) After reserve(), the iterator 'it' may be invalid.\n", errout.str());
}
void pushback11() {
// #2798
check("void f() {\n"
" std::vector ints;\n"
" std::vector::iterator it = ints.begin();\n"
" if (it == ints.begin()) {\n"
" ints.push_back(0);\n"
" } else {\n"
" ints.insert(it,0);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void pushback12() {
// #4197
check("void foo(double s)\n"
"{\n"
" std::vector vec;\n"
" for( std::vector::iterator it = vec.begin(); it != vec.end(); ++it )\n"
" {\n"
" vec.insert( it, s );\n"
" for(unsigned int i = 0; i < 42; i++)\n"
" {}\n"
" *it;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) After insert(), the iterator 'it' may be invalid.\n"
"[test.cpp:9]: (error) After insert(), the iterator 'it' may be invalid.\n", errout.str());
}
void pushback13() {
check("bool Preprocessor::ConcatenateIncludeName(SmallString<128> &FilenameBuffer, SourceLocation &End) {\n"
" unsigned PreAppendSize = FilenameBuffer.size();\n"
" FilenameBuffer.resize(PreAppendSize + CurTok.getLength());\n"
" const char *BufPtr = &FilenameBuffer[PreAppendSize];\n"
" return true;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void insert1() {
check("void f(std::vector &ints)\n"
"{\n"
" std::vector::iterator iter = ints.begin() + 5;\n"
" ints.insert(ints.begin(), 1);\n"
" ++iter;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) After insert(), the iterator 'iter' may be invalid.\n", errout.str());
check("void f()\n"
"{\n"
" std::vector ints;\n"
" std::vector::iterator iter = ints.begin();\n"
" ints.insert(iter, 1);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" std::vector ints;\n"
" std::vector::iterator iter = ints.begin();\n"
" ints.insert(iter, 1);\n"
" ints.insert(iter, 2);\n"
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) After insert(), the iterator 'iter' may be invalid.\n", errout.str());
check("void* f(const std::vector& bars) {\n"
" std::vector::iterator i = bars.begin();\n"
" bars.insert(Bar());\n"
" void* v = &i->foo;\n"
" return v;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) After insert(), the iterator 'i' may be invalid.\n", errout.str());
check("Foo f(const std::vector