/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 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 "errortypes.h"
#include "fixture.h"
#include "helpers.h"
#include "settings.h"
#include "standards.h"
#include "utils.h"
#include
#include
class TestStl : public TestFixture {
public:
TestStl() : TestFixture("TestStl") {}
private:
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).severity(Severity::performance).library("std.cfg").build();
const Settings settings_i = settingsBuilder(settings).certainty(Certainty::inconclusive).build();
const Settings settingsCpp03 = settingsBuilder(settings).cpp(Standards::CPP03).build();
void run() override {
mNewTemplate = true;
TEST_CASE(outOfBounds);
TEST_CASE(outOfBoundsSymbolic);
TEST_CASE(outOfBoundsIndexExpression);
TEST_CASE(outOfBoundsIterator);
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(iterator16);
TEST_CASE(iterator17);
TEST_CASE(iterator18);
TEST_CASE(iterator19);
TEST_CASE(iterator20);
TEST_CASE(iterator21);
TEST_CASE(iterator22);
TEST_CASE(iterator23);
TEST_CASE(iterator24);
TEST_CASE(iterator25); // #9742
TEST_CASE(iterator26); // #9176
TEST_CASE(iterator27); // #10378
TEST_CASE(iterator28); // #10450
TEST_CASE(iterator29);
TEST_CASE(iterator30);
TEST_CASE(iterator31);
TEST_CASE(iteratorExpression);
TEST_CASE(iteratorSameExpression);
TEST_CASE(mismatchingContainerIterator);
TEST_CASE(eraseIteratorOutOfBounds);
TEST_CASE(algorithmOutOfBounds);
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(negativeIndexMultiline);
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(popback1);
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(uselessCalls);
TEST_CASE(stabilityOfChecks); // #4684 cppcheck crash in template function call
TEST_CASE(dereferenceInvalidIterator);
TEST_CASE(dereferenceInvalidIterator2); // #6572
TEST_CASE(dereference_auto);
TEST_CASE(loopAlgoElementAssign);
TEST_CASE(loopAlgoAccumulateAssign);
TEST_CASE(loopAlgoContainerInsert);
TEST_CASE(loopAlgoIncrement);
TEST_CASE(loopAlgoConditional);
TEST_CASE(loopAlgoMinMax);
TEST_CASE(loopAlgoMultipleReturn);
TEST_CASE(invalidContainer);
TEST_CASE(invalidContainerLoop);
TEST_CASE(findInsert);
TEST_CASE(checkKnownEmptyContainer);
TEST_CASE(checkMutexes);
}
struct CheckOptions
{
bool inconclusive = false;
};
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
template
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
const Settings& s = options.inconclusive ? settings_i : settings;
check_(file, line, code, s);
}
template
void check_(const char* file, int line, const char (&code)[size], const Settings& s) {
SimpleTokenizer tokenizer(s, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
CheckStl check;
runChecks(check, tokenizer, *this);
}
// TODO: get rid of this
void check_(const char* file, int line, const std::string& code) {
// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
CheckStl check;
runChecks(check, tokenizer, *this);
}
#define checkNormal(...) checkNormal_(__FILE__, __LINE__, __VA_ARGS__)
template
void checkNormal_(const char* file, int line, const char (&code)[size]) {
// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
CheckStl check;
runChecks(check, tokenizer, *this);
}
void outOfBounds() {
setMultiline();
checkNormal("bool f(const int a, const int b)\n" // #8648
"{\n"
" std::cout resize(n);\n"
" for (int i = 0; i < n; ++i)\n"
" (*PArr)[i] = 1;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("int f() {\n"
" std::vector v;\n"
" std::vector * pv = &v;\n"
" return (*pv).at(42);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:20]: error: Out of bounds access in expression '(*pv).at(42)' because '*pv' is empty. [containerOutOfBounds]\n",
errout_str());
checkNormal("std::string f(const char* DirName) {\n"
" if (DirName == nullptr)\n"
" return {};\n"
" std::string Name{ DirName };\n"
" if (!Name.empty() && Name.back() != '\\\\')\n"
" Name += '\\\\';\n"
" return Name;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("bool f(bool b) {\n"
" std::vector v;\n"
" if (b)\n"
" v.push_back(0);\n"
" for(auto i:v)\n"
" if (v[i] > 0)\n"
" return true;\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:3]: style: Consider using std::any_of algorithm instead of a raw loop. [useStlAlgorithm]\n", errout_str());
checkNormal("std::vector range(int n);\n"
"bool f(bool b) {\n"
" std::vector v;\n"
" if (b)\n"
" v.push_back(1);\n"
" assert(range(v.size()).size() == v.size());\n"
" for(auto i:range(v.size()))\n"
" if (v[i] > 0)\n"
" return true;\n"
" return false;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7:3]: style: Consider using std::any_of algorithm instead of a raw loop. [useStlAlgorithm]\n", errout_str());
checkNormal("bool g();\n"
"int f(int x) {\n"
" std::vector v;\n"
" if (g())\n"
" v.emplace_back(x);\n"
" const auto n = (int)v.size();\n"
" for (int i = 0; i < n; ++i)\n"
" if (v[i] > 0)\n"
" return i;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8:13]: style: Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]\n", errout_str());
checkNormal("bool g();\n"
"int f(int x) {\n"
" std::vector v;\n"
" if (g())\n"
" v.emplace_back(x);\n"
" const auto n = static_cast(v.size());\n"
" for (int i = 0; i < n; ++i)\n"
" if (v[i] > 0)\n"
" return i;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8:13]: style: Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]\n", errout_str());
checkNormal("bool g();\n"
"void f(int x) {\n"
" std::vector v;\n"
" if (g())\n"
" v.emplace_back(x);\n"
" const int n = v.size();\n"
" h(n);\n"
" for (int i = 0; i < n; ++i)\n"
" h(v[i]);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("void foo(const std::vector &v) {\n"
" if(v.size() >=1 && v[0] == 4 && v[1] == 2){}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:38]: warning: Either the condition 'v.size()>=1' is redundant or size of 'v' can be 1. Expression 'v[1]' causes access out of bounds. [containerOutOfBounds]\n"
"[test.cpp:2:17]: note: condition 'v.size()>=1'\n"
"[test.cpp:2:38]: note: Access out of bounds\n", errout_str());
checkNormal("int f(int x, int y) {\n"
" std::vector a = {0,1,2};\n"
" if(x 3)\n"
" return v[v.size() - 3];\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("void f(std::vector v) {\n"
" v[v.size() - 1];\n"
" if (v.size() == 1) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("void f(int n) {\n"
" std::vector v = {1, 2, 3, 4};\n"
" const int i = qMin(n, v.size());\n"
" if (i > 1)\n"
" v[i] = 1;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("void f(std::vector& v, int i) {\n"
" if (i > -1) {\n"
" v.erase(v.begin() + i);\n"
" if (v.empty()) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("void g(const char *, ...) { exit(1); }\n" // #10025
"void f(const char c[]) {\n"
" std::vector v = get();\n"
" if (v.empty())\n"
" g(\"\", c[0]);\n"
" return h(&v[0], v.size()); \n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("void f(int i, std::vector v) {\n" // #9157
" if (i empty())\n"
" v->push_back(1);\n"
" auto x = v->back();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("template \n"
"struct Foo {\n"
" std::array items = {0};\n"
" T maxCount = count;\n"
" explicit Foo(const T& maxValue = (std::numeric_limits::max)()) : maxCount(maxValue) {}\n"
" bool Set(const uint8_t idx) {\n"
" if (CheckBounds(idx) && items[idx] < maxCount) {\n"
" items[idx] += 1;\n"
" return true;\n"
" }\n"
" return false;\n"
" }\n"
" static bool CheckBounds(const uint8_t idx) { return idx < count; }\n"
"};\n"
"void f() {\n"
" Foo x;\n"
" if (x.Set(42U)) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("struct S { void g(std::span& r) const; };\n" // #11828
"int f(const S& s) {\n"
" std::span t;\n"
" s.g(t);\n"
" return t[0];\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("char h() {\n"
" std::string s;\n"
" std::string_view sv(s);\n"
" return s[2];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:13]: error: Out of bounds access in expression 's[2]' because 's' is empty. [containerOutOfBounds]\n", errout_str());
checkNormal("void f() {\n" // #12738
" std::vector v{ 0, 0.1 };\n"
" (void)v[0];\n"
"}\n");
ASSERT_EQUALS("", errout_str());
checkNormal("int f(const std::vector& v) {\n" // #8983
" return v[2];\n"
"}\n"
"int g() {\n"
" return f({});\n"
"}\n"
"int h() {\n"
" return f({ 1, 2 });\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:13]: error: Out of bounds access in 'v[2]', if 'v' size is 2 and '2' is 2 [containerOutOfBounds]\n"
"[test.cpp:2:13]: error: Out of bounds access in expression 'v[2]' because 'v' is empty. [containerOutOfBounds]\n",
errout_str());
checkNormal("int f(int x, const std::vector& v) {\n"
" return x + v[0];\n"
"}\n"
"int g() {\n"
" return f(1, {});\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:17]: error: Out of bounds access in expression 'v[0]' because 'v' is empty. [containerOutOfBounds]\n",
errout_str());
checkNormal("bool f(const std::string_view s) { return s[500] == 'x'; }\n" // #12046
"bool g() { return f(\" \"); }\n");
ASSERT_EQUALS("[test.cpp:1:44]: error: Out of bounds access in 's[500]', if 's' size is 1 and '500' is 500 [containerOutOfBounds]\n",
errout_str());
checkNormal("int main() {\n" // #14342
" const int a[] = { 1, 2, 3 };\n"
" std::span x{ a };\n"
" return x[3];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:13]: error: Out of bounds access in 'x[3]', if 'x' size is 1 and '3' is 3 [containerOutOfBounds]\n",
errout_str());
checkNormal("int main() {\n"
" const char a[] = \"abc\";\n"
" std::string_view x{ a };\n"
" return x[5];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:13]: error: Out of bounds access in 'x[5]', if 'x' size is 4 and '5' is 5 [containerOutOfBounds]\n",
errout_str());
checkNormal("int f(const std::string& v) {\n"
" return v[2];\n"
"}\n"
"int main() {\n"
" std::string_view x{ \"a\" };\n"
" return f(std::string(x));\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:13]: error: Out of bounds access in 'v[2]', if 'v' size is 1 and '2' is 2 [containerOutOfBounds]\n",
errout_str());
checkNormal("std::string f(const std::string& str) {\n" // do not warn, the copy has the same size as 'str'
" std::string outStr = str;\n"
" if (!outStr.empty())\n"
" outStr[0] = 'a';\n"
" for (int i = 0; i < str.size(); ++i) {\n"
" if (outStr[i] == '_')\n"
" outStr[i] = ' ';\n"
" }\n"
" return outStr;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void outOfBoundsSymbolic()
{
check("void foo(std::string textline, int col) {\n"
" if(col > textline.size())\n"
" return false;\n"
" int x = textline[col];\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:2:12] -> [test.cpp:4:21]: (warning) Either the condition 'col>textline.size()' is redundant or 'col' can have the value textline.size(). Expression 'textline[col]' causes access out of bounds. [containerOutOfBounds]\n",
errout_str());
check("void f(const std::vector& v) {\n" // #12742
" for (unsigned i = 0; i < v.size();)\n"
" (void)v[i++];\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void outOfBoundsIndexExpression() {
setMultiline();
checkNormal("void f(std::string s) {\n"
" s[s.size()] = 1;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:3]: error: Out of bounds access of s, index 's.size()' is out of bounds. [containerOutOfBoundsIndexExpression]\n", errout_str());
checkNormal("void f(std::string s) {\n"
" s[s.size()+1] = 1;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:3]: error: Out of bounds access of s, index 's.size()+1' is out of bounds. [containerOutOfBoundsIndexExpression]\n", errout_str());
checkNormal("void f(std::string s) {\n"
" s[1+s.size()] = 1;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:3]: error: Out of bounds access of s, index '1+s.size()' is out of bounds. [containerOutOfBoundsIndexExpression]\n", errout_str());
checkNormal("void f(std::string s) {\n"
" s[x*s.size()] = 1;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:3]: error: Out of bounds access of s, index 'x*s.size()' is out of bounds. [containerOutOfBoundsIndexExpression]\n", errout_str());
checkNormal("bool f(std::string_view& sv) {\n" // #10031
" return sv[sv.size()] == '\\0';\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:12]: error: Out of bounds access of sv, index 'sv.size()' is out of bounds. [containerOutOfBoundsIndexExpression]\n", errout_str());
}
void outOfBoundsIterator() {
check("int f() {\n"
" std::vector v;\n"
" auto it = v.begin();\n"
" return *it;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:13]: (error) Out of bounds access in expression 'it' because 'v' is empty. [containerOutOfBounds]\n",
errout_str());
check("int f() {\n"
" std::vector v;\n"
" v.push_back(0);\n"
" auto it = v.begin() + 1;\n"
" return *it;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:13]: (error) Out of bounds access in 'it', if 'v' size is 1 and 'it' is at position 1 from the beginning [containerOutOfBounds]\n",
errout_str());
check("int f() {\n"
" std::vector v;\n"
" v.push_back(0);\n"
" auto it = v.end() - 1;\n"
" return *it;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("int f() {\n"
" std::vector v;\n"
" v.push_back(0);\n"
" auto it = v.end() - 2;\n"
" return *it;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:13]: (error) Out of bounds access in 'it', if 'v' size is 1 and 'it' is at position 2 from the end [containerOutOfBounds]\n",
errout_str());
check("void g(int);\n"
"void f(std::vector x) {\n"
" std::map m;\n"
" if (!m.empty()) {\n"
" g(m.begin()->second);\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" std::vector vec;\n"
" std::vector::iterator it = vec.begin();\n"
" *it = 1;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:6]: (error) Out of bounds access in expression 'it' because 'vec' is empty. [containerOutOfBounds]\n",
errout_str());
check("void f() {\n"
" std::vector vec;\n"
" auto it = vec.begin();\n"
" *it = 1;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:6]: (error) Out of bounds access in expression 'it' because 'vec' is empty. [containerOutOfBounds]\n",
errout_str());
}
void iterator1() {
check("void f()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" for (std::list::iterator it = l1.begin(); it != l2.end(); ++it)\n"
" { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:40]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n",
errout_str());
check("void f()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" for (std::list::iterator it = l1.begin(); l2.end() != it; ++it)\n"
" { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:52]: (error) Iterators of different containers 'l2' and 'l1' are used together. [mismatchingContainers]\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"
"}\n");
ASSERT_EQUALS("", errout_str());
// Same check with reverse iterator
check("void f()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" for (std::list::const_reverse_iterator it = l1.rbegin(); it != l2.rend(); ++it)\n"
" { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:54]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n",
errout_str());
}
void iterator2() {
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it = l1.begin();\n"
" while (it != l2.end())\n"
" {\n"
" ++it;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:35]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it = l1.begin();\n"
" while (l2.end() != it)\n"
" {\n"
" ++it;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:12]: (error) Iterators of different containers 'l2' and 'l1' are used together. [mismatchingContainers]\n",
errout_str());
}
void iterator3() {
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it = l1.begin();\n"
" l2.insert(it, 0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:5]: (error) Same iterator is used with different containers 'l1' and 'l2'. [iterators1]\n"
"[test.cpp:6:5]: (error) Iterator 'it' referring to container 'l1' is used with container 'l2'. [mismatchingContainerIterator]\n",
errout_str());
check("void foo() {\n" // #5803
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it = l1.begin();\n"
" l2.insert(it, l1.end());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void foo() {\n" // #7658
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it = l1.begin();\n"
" std::list::iterator end = l1.end();\n"
" l2.insert(it, end);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// only warn for insert when there are preciself 2 arguments.
check("void foo() {\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it = l1.begin();\n"
" l2.insert(it);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void foo() {\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it = l1.begin();\n"
" l2.insert(it,0,1);\n"
"}\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"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator5() {
check("void foo(std::vector ints1, std::vector ints2)\n"
"{\n"
" std::vector::iterator it = std::find(ints1.begin(), ints2.end(), 22);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:47]: (error) Iterators of different containers 'ints1' and 'ints2' are used together. [mismatchingContainers]\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"
"}\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"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Iterators of different containers are used together.\n", "", errout_str());
}
void iterator7() {
check("void foo(std::vector ints1, std::vector ints2)\n"
"{\n"
" std::vector::iterator it = std::inplace_merge(ints1.begin(), std::advance(ints1.rbegin(), 5), ints2.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:56]: (error) Iterators of different containers 'ints1' and 'ints2' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo(std::vector ints1, std::vector ints2)\n"
"{\n"
" std::vector::iterator it = std::inplace_merge(ints1.begin(), std::advance(ints2.rbegin(), 5), ints1.end());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator8() {
check("void foo(std::vector ints1, std::vector ints2)\n"
"{\n"
" std::vector::iterator it = std::find_first_of(ints1.begin(), ints2.end(), ints1.begin(), ints1.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:56]: (error) Iterators of different containers 'ints1' and 'ints2' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo(std::vector ints1, std::vector ints2)\n"
"{\n"
" std::vector::iterator it = std::find_first_of(ints1.begin(), ints1.end(), ints2.begin(), ints1.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:84]: (error) Iterators of different containers 'ints2' and 'ints1' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo(std::vector ints1, std::vector ints2)\n"
"{\n"
" std::vector::iterator it = std::find_first_of(foo.bar.begin(), foo.bar.end()-6, ints2.begin(), ints1.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:90]: (error) Iterators of different containers 'ints2' and 'ints1' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo(std::vector ints1, std::vector ints2)\n"
"{\n"
" std::vector::iterator it = std::find_first_of(ints1.begin(), ints1.end(), ints2.begin(), ints2.end());\n"
"}\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"
"}\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:26]: (error) Iterators of different containers 'tp3' and 'a' are used together. [mismatchingContainers]\n"
"[test.cpp:5:26]: (error) Iterators of different containers 'tp4' and 'b' are used together. [mismatchingContainers]\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"
"}\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"
"}\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"
"}\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"
"}\n");
ASSERT_EQUALS("[test.cpp:5:39]: (error) Iterators of different containers 's1' and 's2' are used together. [mismatchingContainers]\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"
"}\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()) { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:45]: (error) Iterators of different containers 'map1' and 'map2' are used together. [mismatchingContainers]\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) { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:9]: (error) Iterators of different containers 'map2' and 'map1' are used together. [mismatchingContainers]\n",
errout_str());
check("void f(std::string &s) {\n"
" int pos = s.find(x);\n"
" s.erase(pos);\n"
" s.erase(pos);\n"
"}\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"
"}\n");
ASSERT_EQUALS("[test.cpp:8:10]: (error) Iterators of different containers 't' and 'a' are used together. [mismatchingContainers]\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"
" ++it;\n"
" it = t.begin();\n"
" while (it!=t.end())\n"
" ++it;\n"
"}\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"
" ++it;\n"
" while (!z && it!=t.end())\n"
" ++it;\n"
"}\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"
"}\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"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator16() {
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l2.end();\n"
" while (it1 != it2)\n"
" {\n"
" ++it1;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:36]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.end();\n"
" std::list::iterator it2 = l2.begin();\n"
" while (it2 != it1)\n"
" {\n"
" ++it2;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:36]: (error) Iterators of different containers 'l2' and 'l1' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it2 = l2.end();\n"
" std::list::iterator it1 = l1.begin();\n"
" while (it1 != it2)\n"
" {\n"
" ++it1;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:36]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo()\n"
"{\n"
" std::set l1;\n"
" std::set l2(10, 4);\n"
" std::set::iterator it1 = l1.begin();\n"
" std::set::iterator it2 = l2.find(4);\n"
" while (it1 != it2)\n"
" {\n"
" ++it1;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:35]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n",
errout_str());
}
void iterator17() {
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l1.end();\n"
" { it2 = l2.end(); }\n"
" while (it1 != it2)\n"
" {\n"
" ++it1;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:36]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l1.end();\n"
" while (it1 != it2)\n"
" {\n"
" ++it1;\n"
" }\n"
" it2 = l2.end();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l1.end();\n"
" it1 = l2.end();\n"
" it1 = l1.end();\n"
" if (it1 != it2)\n"
" {\n"
" ++it1;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l1.end();\n"
" { it2 = l2.end(); }\n"
" it2 = l1.end();\n"
" { it2 = l2.end(); }\n"
" while (it1 != it2)\n"
" {\n"
" ++it1;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:36]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n",
errout_str());
}
void iterator18() {
check("void foo(std::list l1, std::list l2)\n"
"{\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l1.end();\n"
" while (++it1 != --it2)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void foo(std::list l1, std::list l2)\n"
"{\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l1.end();\n"
" while (it1++ != --it2)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void foo(std::list l1, std::list l2)\n"
"{\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l1.end();\n"
" if (--it2 > it1++)\n"
" {\n"
" }\n"
"}\n");
TODO_ASSERT_EQUALS("", "[test.cpp:5:15]: (error) Dangerous comparison using operator< on iterator. [stlBoundaries]\n", errout_str());
}
void iterator19() {
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list::iterator it1 = l1.begin();\n"
" {\n"
" std::list l1;\n"
" if (it1 != l1.end())\n"
" {\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:7:13] -> [test.cpp:4:36]: (error) Same iterator is used with containers 'l1' that are temporaries or defined in different scopes. [iterators3]\n",
errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list::iterator it1 = l1.begin();\n"
" {\n"
" std::list l1;\n"
" if (l1.end() > it1)\n"
" {\n"
" }\n"
" }\n"
"}\n");
TODO_ASSERT_EQUALS(
"[test.cpp:7] -> [test.cpp:4]: (error) Same iterator is used with containers 'l1' that are defined in different scopes.\n",
"[test.cpp:7:19] -> [test.cpp:7:13]: (error) Same iterator is used with containers 'l1' that are temporaries or defined in different scopes. [iterators3]\n"
"[test.cpp:7:22]: (error) Dangerous comparison using operator< on iterator. [stlBoundaries]\n",
errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list::iterator it1 = l1.begin();\n"
" {\n"
" std::list l1;\n"
" std::list::iterator it2 = l1.begin();\n"
" if (it1 != it2)\n"
" {\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:8:13] -> [test.cpp:4:36]: (error) Same iterator is used with containers 'l1' that are temporaries or defined in different scopes. [iterators3]\n",
errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list::iterator it1 = l1.begin();\n"
" {\n"
" std::list l1;\n"
" std::list::iterator it2 = l1.begin();\n"
" if (it2 != it1)\n"
" {\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:8:13] -> [test.cpp:7:40]: (error) Same iterator is used with containers 'l1' that are temporaries or defined in different scopes. [iterators3]\n",
errout_str());
check("std::set g() {\n"
" static const std::set s = {1};\n"
" return s;\n"
"}\n"
"void f() {\n"
" if (g().find(2) == g().end()) {}\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:6:17] -> [test.cpp:6:10]: (error) Same iterator is used with containers 'g()' that are temporaries or defined in different scopes. [iterators3]\n",
errout_str());
check("std::set f() {\n" // #5804
" std::set s;\n"
" return s;\n"
"}\n"
"void g() {\n"
" for (std::set::iterator it = f().begin(); it != f().end(); ++it) {}\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:6:53] -> [test.cpp:6:41]: (error) Same iterator is used with containers 'f()' that are temporaries or defined in different scopes. [iterators3]\n",
errout_str());
}
void iterator20() {
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l2.begin();\n"
" it1 = it2;\n"
" while (it1 != l1.end())\n"
" {\n"
" ++it1;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:36]: (error) Iterators of different containers 'l2' and 'l1' are used together. [mismatchingContainers]\n",
errout_str());
check("std::list l3;\n"
"std::list::iterator bar()\n"
"{\n"
" return l3.end();\n"
"}\n"
"void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.begin();\n"
" std::list::iterator it2 = l2.begin();\n"
" it1 = bar();\n"
" while (it1 != it2)\n"
" {\n"
" ++it1;\n"
" }\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:13] -> [test.cpp:10] -> [test.cpp:11]: (error) Comparison of iterators from containers 'l1' and 'l2'.\n", "", errout_str());
}
void iterator21() {
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.end();\n"
" std::list::iterator it2 = l2.begin();\n"
" if (it1 != it2)\n"
" {\n"
" }\n"
" if (it2 != it1)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:36]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n"
"[test.cpp:6:36]: (error) Iterators of different containers 'l2' and 'l1' are used together. [mismatchingContainers]\n",
errout_str());
check("void foo()\n"
"{\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list::iterator it1 = l1.end();\n"
" std::list::iterator it2 = l2.begin();\n"
" if (it1 != it2 && it1 != it2)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:36]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n"
"[test.cpp:5:36]: (error) Iterators of different containers 'l1' and 'l2' are used together. [mismatchingContainers]\n", // duplicate
errout_str());
}
void iterator22() { // #7107
check("void foo() {\n"
" std::list &l = x.l;\n"
" std::list::iterator it = l.find(123);\n"
" x.l.erase(it);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator23() { // #9550
check("struct A {\n"
" struct B {\n"
" bool operator==(const A::B& b) const;\n"
" int x;\n"
" int y;\n"
" int z;\n"
" };\n"
"};\n"
"bool A::B::operator==(const A::B& b) const {\n"
" return std::tie(x, y, z) == std::tie(b.x, b.y, b.z);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator24() {
// #9556
check("void f(int a, int b) {\n"
" if (&a == &b) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f(int a, int b) {\n"
" if (std::for_each(&a, &b + 1, [](auto) {})) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:22]: (error) Iterators of different containers 'a' and 'b' are used together. [mismatchingContainers]\n",
errout_str());
check("void f(int a, int b) {\n"
" if (std::for_each(&a, &b, [](auto) {})) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:22]: (error) Iterators of different containers 'a' and 'b' are used together. [mismatchingContainers]\n",
errout_str());
check("void f(int a) {\n"
" if (std::for_each(&a, &a, [](auto) {})) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:21]: (style) Same iterators expression are used for algorithm. [sameIteratorExpression]\n", errout_str());
check("void f(int a) {\n"
" if (std::for_each(&a, &a + 1, [](auto) {})) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator25() {
// #9742
check("struct S {\n"
" std::vector& v;\n"
"};\n"
"struct T {\n"
" bool operator()(const S& lhs, const S& rhs) const {\n"
" return &lhs.v != &rhs.v;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}
void iterator26() { // #9176
check(
"#include \n"
"int main()\n"
"{"
" std::map m{ {\"a\", 1} };\n"
" if (auto iter = m.find(\"x\"); iter != m.end()) {\n"
" return iter->second;\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator27() {
// #10378
check("struct A {\n"
" int a;\n"
" int b;\n"
"};\n"
"int f(std::map m) {\n"
" auto it = m.find( 1 );\n"
" const int a( it == m.cend() ? 0 : it->second.a );\n"
" const int b( it == m.cend() ? 0 : it->second.b );\n"
" return a + b;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator28()
{
// #10450
check("struct S {\n"
" struct Private {\n"
" std::list l;\n"
" };\n"
" std::unique_ptr p;\n"
" int foo();\n"
"};\n"
"int S::foo() {\n"
" for(auto iter = p->l.begin(); iter != p->l.end(); ++iter) {\n"
" if(*iter == 1) {\n"
" p->l.erase(iter);\n"
" return 1;\n"
" }\n"
" }\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10:24]: (style) Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]\n", errout_str());
}
void iterator29()
{
// #11511
check("std::vector& g();\n"
"void f() {\n"
" auto v = g();\n"
" auto it = g().begin();\n"
" while (it != g().end())\n"
" it = v.erase(it);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:14]: (error) Iterator 'it' referring to container 'g()' is used with container 'v'. [mismatchingContainerIterator]\n", errout_str());
check("std::vector& g(int);\n"
"void f(int i, int j) {\n"
" auto& r = g(i);\n"
" auto it = g(j).begin();\n"
" while (it != g(j).end())\n"
" it = r.erase(it);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:14]: (error) Iterator 'it' referring to container 'g(j)' is used with container 'r'. [mismatchingContainerIterator]\n", errout_str());
check("std::vector& g();\n"
"void f() {\n"
" auto& r = g();\n"
" auto it = g().begin();\n"
" while (it != g().end())\n"
" it = r.erase(it);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator30()
{
check("struct S {\n" // #12641
" bool b;\n"
" std::list A, B;\n"
" void f();\n"
"};\n"
"void S::f() {\n"
" std::list::iterator i = (b ? B : A).begin();\n"
" while (i != (b ? B : A).end()) {\n"
" ++i;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void iterator31()
{
check("struct S {\n" // #13327
" std::string a;\n"
"};\n"
"struct T {\n"
" S s;\n"
"};\n"
"bool f(const S& s) {\n"
" std::string b;\n"
" return s.a.c_str() == b.c_str();\n"
"}\n"
"bool g(const T& t) {\n"
" std::string b;\n"
" return t.s.a.c_str() == b.c_str();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9:13]: (error) Iterators of different containers 's.a' and 'b' are used together. [mismatchingContainers]\n"
"[test.cpp:13:15]: (error) Iterators of different containers 't.s.a' and 'b' are used together. [mismatchingContainers]\n",
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:22]: (error) Iterators of different containers 'f()' and 'g()' are used together. [mismatchingContainers]\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:9]: (error) Iterators of different containers 'f()' and 'g()' are used together. [mismatchingContainers]\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:18]: (error) Iterators of different containers 'f()' and 'g()' are used together. [mismatchingContainers]\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:26]: (error) Iterators of different containers 'A().f()' and 'A().g()' are used together. [mismatchingContainers]\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:27]: (error) Iterators of different containers 'A{}.f()' and 'A{}.g()' are used together. [mismatchingContainers]\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:28]: (warning) Iterators to containers from different expressions 'f()' and 'g()' are used together. [mismatchingContainerExpression]\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"
"void foo() {\n"
" auto it = f().end() - 1;\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("[test.cpp:9:30]: (error) Dereference of an invalid iterator: f().end()+1 [derefInvalidIterator]\n", errout_str());
check("std::vector& f();\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("[test.cpp:4:32]: (error) Dereference of an invalid iterator: f().end()+1 [derefInvalidIterator]\n"
"[test.cpp:6:34]: (error) Dereference of an invalid iterator: f().end()+1 [derefInvalidIterator]\n",
errout_str());
check("std::vector& f();\n"
"void foo() {\n"
" if(std::begin(f()) == std::end(f())) {}\n"
" if(std::begin(f()) == std::end(f())+1) {}\n"
" if(std::begin(f())+1 == std::end(f())) {}\n"
" if(std::begin(f())+1 == std::end(f())+1) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:40]: (error) Dereference of an invalid iterator: std::end(f())+1 [derefInvalidIterator]\n"
"[test.cpp:6:42]: (error) Dereference of an invalid iterator: std::end(f())+1 [derefInvalidIterator]\n",
errout_str());
check("template\n"
"std::vector& f();\n"
"void foo() {\n"
" if(f().begin() == f().end()) {}\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(std::list a, std::list b) {\n"
" if (*a.begin() == *b.begin()) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void foo() {\n"
" if(f().begin(1) == f().end()) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void foo(const uint8_t* data, const uint32_t dataLength) {\n"
" const uint32_t minimumLength = sizeof(uint16_t) + sizeof(uint16_t);\n"
" if (dataLength >= minimumLength) {\n"
" char* payload = new char[dataLength - minimumLength];\n"
" std::copy(&data[minimumLength], &data[dataLength], payload);\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("bool f(const std::vector& a, const std::vector& b) {\n" // #11469
" return (a.begin() - a.end()) == (b.begin() - b.end());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("struct S {\n" // #11469
" const std::vector* vec() const { return &v; }\n"
" const std::vector v;\n"
"};\n"
"void f(const S& a, const S& b) {\n"
" if (a.vec()->begin() - a.vec()->end() != b.vec()->begin() - b.vec()->end()) {}\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:26]: (style) Same iterators expression are used for algorithm. [sameIteratorExpression]\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:28]: (style) Same iterators expression are used for algorithm. [sameIteratorExpression]\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:24]: (style) Same iterators expression are used for algorithm. [sameIteratorExpression]\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:26]: (style) Same iterators expression are used for algorithm. [sameIteratorExpression]\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:20]: (style) Same iterators expression are used for algorithm. [sameIteratorExpression]\n", errout_str());
check("void f(std::vector::iterator it) {\n"
" std::for_each(it, it, [](int){});\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:19]: (style) Same iterators expression are used for algorithm. [sameIteratorExpression]\n", errout_str());
}
void mismatchingContainerIterator() {
check("std::vector to_vector(int value) {\n"
" std::vector a, b;\n"
" a.insert(b.end(), value);\n"
" return a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:5]: (error) Iterator 'b.end()' referring to container 'b' is used with container 'a'. [mismatchingContainerIterator]\n", errout_str());
check("std::vector f(std::vector a, std::vector b) {\n"
" a.erase(b.begin());\n"
" return a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:5]: (error) Iterator 'b.begin()' referring to container 'b' is used with container 'a'. [mismatchingContainerIterator]\n", errout_str());
// #9973
check("void f() {\n"
" std::list l1;\n"
" std::list l2;\n"
" std::list& l = l2;\n"
" for (auto it = l.begin(); it != l.end(); ++it) {\n"
" if (*it == 1) {\n"
" l.erase(it);\n"
" break;\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:23]: (style) Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]\n", errout_str());
// #10012
check("struct a {\n"
" int b;\n"
" int end() { return b; }\n"
"};\n"
"void f(a c, a d) {\n"
" if (c.end() == d.end()) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// #10467
check("void f(std::array& A) {\n"
" for (auto& a : A) {\n"
" auto it = std::find_if(a.begin(), a.end(), \n"
" [](auto i) { return i == 0; });\n"
" if (it != a.end()) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// #10604
check("struct S {\n"
" std::vector v;\n"
"};\n"
"void f(S& s, int m) {\n"
" s.v.erase(s.v.begin() + m);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// #11093
check("struct S {\n"
" std::vector v1, v2;\n"
" void f(bool b) {\n"
" std::vector& v = b ? v1 : v2;\n"
" v.erase(v.begin());\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
// #12377
check("void f(bool b) {\n"
" std::vector *pv;\n"
" if (b) {\n"
" std::vector& r = get1();\n"
" pv = &r;\n"
" }\n"
" else {\n"
" std::vector& r = get2();\n"
" pv = &r;\n"
" }\n"
" std::vector::iterator it = pv->begin();\n"
" it = pv->erase(it, it + 2);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("struct S {\n"
" std::vector v;\n"
" void f() {\n"
" std::vector* p = &v;\n"
" p->insert(std::find(p->begin(), p->end(), 0), 1);\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
check("struct S {\n"
" std::vector v;\n"
" void f(int i) {\n"
" std::vector* p = &v;\n"
" if (p->size() > i)\n"
" p->erase(p->begin() + i, p->end());\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
// #11067
check("struct S {\n"
" std::vector v;\n"
" std::list li;\n"
" void f();\n"
"};\n"
"void S::f() {\n"
" v.erase(*li.begin());\n"
" li.pop_front();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f(std::set& a, std::stack& b) {\n"
" while (!b.empty()) {\n"
" a.erase(b.top());\n"
" b.pop();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f(std::vector& a, std::vector& b) {\n"
" auto it = b.begin();\n"
" a.erase(*it);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("namespace N {\n" // #12443
" std::vector v;\n"
"}\n"
"using namespace N;\n"
"void f() {\n"
" auto it = std::find(v.begin(), v.end(), 0);\n"
" if (it != N::v.end()) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f(void* p) {\n" // #12445
" std::vector&v = *(std::vector*)(p);\n"
" v.erase(v.begin(), v.end());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// #13408
check("void f(const std::vector& v) {\n"
" for (const auto& i : v) {\n"
" if (std::distance(&*v.cbegin(), &i)) {}\n"
" } \n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("std::string g1();\n" // #12520
"const std::string& g2();\n"
"void f() {\n"
" g1().erase(g1().begin());\n"
" g2().erase(g2().begin());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:7]: (error) Iterator 'g1().begin()' referring to temporary container 'g1()' is used with temporary container 'g1()'. [mismatchingContainerIterator]\n",
errout_str());
}
void eraseIteratorOutOfBounds() {
check("void f() {\n"
" std::vector v;\n"
" v.erase(v.begin());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:7]: (error) Calling function 'erase()' on the iterator 'v.begin()' which is out of bounds. [eraseIteratorOutOfBounds]\n", errout_str());
check("void f() {\n"
" std::vector v;\n"
" v.erase(v.end());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:7]: (error) Calling function 'erase()' on the iterator 'v.end()' which is out of bounds. [eraseIteratorOutOfBounds]\n", errout_str());
check("void f() {\n"
" std::vector v;\n"
" auto it = v.begin();\n"
" v.erase(it);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:7]: (error) Calling function 'erase()' on the iterator 'it' which is out of bounds. [eraseIteratorOutOfBounds]\n", errout_str());
check("void f() {\n"
" std::vector v{ 1, 2, 3 };\n"
" auto it = v.end();\n"
" v.erase(it);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:7]: (error) Calling function 'erase()' on the iterator 'it' which is out of bounds. [eraseIteratorOutOfBounds]\n", errout_str());
check("void f() {\n"
" std::vector v{ 1, 2, 3 };\n"
" auto it = v.begin();\n"
" v.erase(it);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" std::vector v{ 1, 2, 3 };\n"
" v.erase(v.end() - 1);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" std::vector v{ 1, 2, 3 };\n"
" v.erase(v.begin() - 1);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:7]: (error) Calling function 'erase()' on the iterator 'v.begin()-1' which is out of bounds. [eraseIteratorOutOfBounds]\n"
"[test.cpp:3:23]: (error) Dereference of an invalid iterator: v.begin()-1 [derefInvalidIterator]\n",
errout_str());
check("void f(std::vector& v, std::vector::iterator it) {\n"
" if (it == v.end()) {}\n"
" v.erase(it);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:7]: (warning) Either the condition 'it==v.end()' is redundant or function 'erase()' is called on the iterator 'it' which is out of bounds. [eraseIteratorOutOfBoundsCond]\n",
errout_str());
check("void f() {\n"
" std::vector v;\n"
" ((v).erase)(v.begin());\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:10]: (error) Calling function 'erase()' on the iterator 'v.begin()' which is out of bounds. [eraseIteratorOutOfBounds]\n",
errout_str());
}
void algorithmOutOfBounds()
{
check("void f() {\n"
" const std::deque d0{1,2,3,4,5,6,7,8,9,10};\n"
" const std::deque d1{1,2,3,4,5,6};\n"
" if(std::equal(d0.cbegin(), d0.cend(), d1.cbegin())) {}\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4:52]: (error) The algorithm 'std::equal' accesses 10 elements through the iterator 'd1.cbegin()' but only 6 elements are available. [algorithmOutOfBounds]\n",
errout_str());
check("void f() {\n"
" const std::deque d0{1,2,3,4,5,6};\n"
" const std::deque d1{1,2,3,4,5,6};\n"
" if(std::equal(d0.cbegin(), d0.cend(), d1.cbegin())) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// two-range overload does not access the second range out of bounds
check("void f() {\n"
" const std::deque d0{1,2,3,4,5,6,7,8,9,10};\n"
" const std::deque d1{1,2,3,4,5,6};\n"
" if(std::equal(d0.cbegin(), d0.cend(), d1.cbegin(), d1.cend())) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::vector v1(3);\n"
" std::copy(v0.begin(), v0.end(), v1.begin());\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4:45]: (error) The algorithm 'std::copy' accesses 5 elements through the iterator 'v1.begin()' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::vector v1(5);\n"
" std::copy(v0.begin(), v0.end(), v1.begin());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// iterator arithmetic
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::vector v1(6);\n"
" std::copy(v0.begin(), v0.end(), v1.begin() + 3);\n"
" std::copy(v0.begin() + 3, v0.end(), v1.begin());\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4:48]: (error) The algorithm 'std::copy' accesses 5 elements through the iterator 'v1.begin()+3' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
// don't warn when using iterator adaptors
check("void f() {\n"
" const std::deque d0{1,2,3,4,5,6,7,8,9,10};\n"
" std::deque d1;\n"
" std::copy(d0.cbegin(), d0.cend(), std::back_inserter(d1));\n"
" std::copy(d0.cbegin(), d0.cend(), std::inserter(d1, d1.begin()));\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// algorithms that access at most last-first elements are inconclusive
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::vector v1(3);\n"
" std::copy_if(v0.begin(), v0.end(), v1.begin(), [](int i) { return i != 3; });\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::vector v1(3);\n"
" std::copy_if(v0.begin(), v0.end(), v1.begin(), [](int i) { return i != 3; });\n"
"}\n",
dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS(
"[test.cpp:4:48]: (warning, inconclusive) The algorithm 'std::copy_if' may access up to 5 elements through the iterator 'v1.begin()' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::vector v1(3);\n"
" std::transform(v0.begin(), v0.end(), v1.begin(), [](int i) { return i * 2; });\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4:50]: (error) The algorithm 'std::transform' accesses 5 elements through the iterator 'v1.begin()' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
// binary transform reads the third argument and writes the fourth argument
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" const std::vector v1{1,2,3};\n"
" std::vector v2(5);\n"
" std::transform(v0.begin(), v0.end(), v1.begin(), v2.begin(), [](int a, int b) { return a + b; });\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:5:50]: (error) The algorithm 'std::transform' accesses 5 elements through the iterator 'v1.begin()' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" const std::vector v1{1,2,3,4,5};\n"
" std::vector v2(3);\n"
" std::transform(v0.begin(), v0.end(), v1.begin(), v2.begin(), [](int a, int b) { return a + b; });\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:5:62]: (error) The algorithm 'std::transform' accesses 5 elements through the iterator 'v2.begin()' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
// copying a range within the same container
check("void f() {\n"
" std::vector v{1,2,3,4,5,6,7,8,9,10};\n"
" std::copy(v.begin(), v.begin() + 3, v.begin() + 7);\n"
" std::copy(v.begin(), v.begin() + 4, v.begin() + 7);\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4:51]: (error) The algorithm 'std::copy' accesses 4 elements through the iterator 'v.begin()+7' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
// unknown container sizes
check("void f(const std::vector& v0, std::vector& v1) {\n"
" std::copy(v0.begin(), v0.end(), v1.begin());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// don't warn for iterator variables when the container size changes before the algorithm is called
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::list l1(3);\n"
" auto it = l1.begin();\n"
" l1.resize(10);\n"
" std::copy(v0.begin(), v0.end(), it);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// iterator variables carry the container size
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::vector v1(3);\n"
" auto it = v1.begin();\n"
" std::copy(v0.begin(), v0.end(), it);\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:5:37]: (error) The algorithm 'std::copy' accesses 5 elements through the iterator 'it' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
// ..and the size is the size at the call, not at the creation of the iterator
check("void f() {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::list l1(10);\n"
" auto it = l1.begin();\n"
" l1.resize(3);\n"
" std::copy(v0.begin(), v0.end(), it);\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:6:37]: (error) The algorithm 'std::copy' accesses 5 elements through the iterator 'it' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
// conditional container size
check("void f(std::vector& v) {\n"
" const std::vector v0{1,2,3,4,5};\n"
" if (v.size() == 3)\n"
" std::copy(v0.begin(), v0.end(), v.begin());\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:3:18] -> [test.cpp:4:48]: (warning) Either the condition 'v.size()==3' is redundant or the algorithm 'std::copy' accesses 5 elements through the iterator 'v.begin()' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
check("void f(std::vector& v) {\n"
" const std::vector v0{1,2,3,4,5};\n"
" if (v.size() < 5)\n"
" std::copy(v0.begin(), v0.end(), v.begin());\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:3:18] -> [test.cpp:4:48]: (warning) Either the condition 'v.size()5' is redundant or the algorithm 'std::fill_n' accesses 6 elements through the iterator 'v.begin()' but only 5 elements are available. [algorithmOutOfBounds]\n",
errout_str());
// an upper bound of the count cannot tell whether the access is out of bounds
check("void f(int n) {\n"
" std::vector v(3);\n"
" if (n < 5)\n"
" std::fill_n(v.begin(), n, 0);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// a lower bound of the container size cannot tell whether the access is out of bounds
check("void f(std::vector& v) {\n"
" const std::vector v0{1,2,3,4,5};\n"
" if (v.size() >= 4)\n"
" std::copy(v0.begin(), v0.end(), v.begin());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// unknown count
check("void f(std::vector& v, int n) {\n"
" std::fill_n(v.begin(), n, 0);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// all container size values are checked, not only the first one
check("void f(bool b) {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::vector v1;\n"
" if (b)\n"
" v1.resize(3);\n"
" else\n"
" v1.resize(10);\n"
" std::copy(v0.begin(), v0.end(), v1.begin());\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:8:45]: (error) The algorithm 'std::copy' accesses 5 elements through the iterator 'v1.begin()' but only 3 elements are available. [algorithmOutOfBounds]\n",
errout_str());
check("void f(bool b) {\n"
" const std::vector v0{1,2,3,4,5};\n"
" std::vector v1;\n"
" if (b)\n"
" v1.resize(5);\n"
" else\n"
" v1.resize(10);\n"
" std::copy(v0.begin(), v0.end(), v1.begin());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// all iterator position values are checked as well
check("void f(bool b) {\n"
" const std::vector v0{1,2,3,4};\n"
" std::vector v1(5);\n"
" auto it = b ? v1.begin() : v1.begin() + 3;\n"
" std::copy(v0.begin(), v0.end(), it);\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:5:37]: (error) The algorithm 'std::copy' accesses 4 elements through the iterator 'it' but only 2 elements are available. [algorithmOutOfBounds]\n",
errout_str());
// do not combine possible values on both sides
check("void f(bool b, std::vector& v0, std::vector& v1) {\n"
" if (b) v0.resize(5); else v0.resize(2);\n"
" if (b) v1.resize(3); else v1.resize(10);\n"
" std::copy(v0.begin(), v0.end(), v1.begin());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// all source range values are checked against the preferred destination values
check("void f(bool b) {\n"
" std::vector v0;\n"
" if (b)\n"
" v0.resize(3);\n"
" else\n"
" v0.resize(10);\n"
" std::vector v1(5);\n"
" std::copy(v0.begin(), v0.end(), v1.begin());\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:8:45]: (error) The algorithm 'std::copy' accesses 10 elements through the iterator 'v1.begin()' but only 5 elements are available. [algorithmOutOfBounds]\n",
errout_str());
check("void f(bool b) {\n"
" std::vector v0;\n"
" if (b)\n"
" v0.resize(3);\n"
" else\n"
" v0.resize(5);\n"
" std::vector v1(5);\n"
" std::copy(v0.begin(), v0.end(), v1.begin());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// all count values are checked as well
check("void f(bool b) {\n"
" std::vector v(5);\n"
" const int n = b ? 3 : 10;\n"
" std::fill_n(v.begin(), n, 0);\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4:24]: (error) The algorithm 'std::fill_n' accesses 10 elements through the iterator 'v.begin()' but only 5 elements are available. [algorithmOutOfBounds]\n",
errout_str());
// an iterator into a nested container does not carry the outer container's size
check("std::array f(const std::array& a) {\n"
" std::array res;\n"
" std::copy(a.begin(), a.end(), res[0].begin());\n"
" return res;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" const std::vector v0{1,2,3};\n"
" std::vector v1(1, std::vector(5));\n"
" std::copy(v0.begin(), v0.end(), v1[0].begin());\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
// Dereferencing invalid pointer
void dereference() {
check("void f()\n"
"{\n"
" std::vector ints{1,2,3,4,5};\n"
" std::vector::iterator iter;\n"
" iter = ints.begin() + 2;\n"
" ints.erase(iter);\n"
" std::cout [test.cpp:3] -> [test.cpp:7]: (error) Using iterator to local container 'ints' that may be invalid.\n", "[test.cpp:5:22] -> [test.cpp:6:10] -> [test.cpp:3:22] -> [test.cpp:7:20]: (error, inconclusive) Using iterator to local container 'ints' that may be invalid. [invalidContainer]\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"
"}\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 [test.cpp:5:10] -> [test.cpp:3:22] -> [test.cpp:6:20]: (error, inconclusive) Using iterator to local container 'ints' that may be invalid. [invalidContainer]\n", errout_str());
check("void f() {\n"
" auto x = *myList.begin();\n"
" myList.erase(x);\n"
" auto b = x.first;\n"
"}\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"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void STLSize() {
check("void foo()\n"
"{\n"
" std::vector foo;\n"
" for (unsigned int ii = 0; ii [test.cpp:8:22]: (error) Iterator 'it' used after element has been erased. [eraseDereference]\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"
"}\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"
"}\n");
ASSERT_EQUALS("[test.cpp:3:5] -> [test.cpp:5:18]: (error) Iterator 'it' used after element has been erased. [eraseDereference]\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"
"}\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"
"}\n");
ASSERT_EQUALS("[test.cpp:3:5] -> [test.cpp:5:18]: (error) Iterator 'it' used after element has been erased. [eraseDereference]\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"
"}\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"
"}\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"
"}\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"
"}\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"
"}\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" // do not warn if cpp standard is < c++17
{
Settings s = settings;
const char code[] = "void f1(std::set& s, const LargeType& x) {\n"
" if (s.find(x) == s.end()) {\n"
" s.insert(x);\n"
" }\n"
"}\n";
s.standards.cpp = Standards::CPP11;
check(code, s);
ASSERT_EQUALS("", errout_str());
s.standards.cpp = Standards::CPP14;
check(code, s);
ASSERT_EQUALS("", errout_str());
s.standards.cpp = Standards::CPP17;
check(code, s);
ASSERT_EQUALS("[test.cpp:3:18]: (performance) Searching before insertion is not necessary. [stlFindInsert]\n", errout_str());
}
{ // #10558
Settings s = settings;
s.standards.cpp = Standards::CPP03;
check("void foo() {\n"
" std::map x;\n"
" int data = 0;\n"
" for(int i=0; i