diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 63491d85f..4069a164a 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -1697,6 +1697,24 @@ private: ASSERT_EQUALS( "[test.cpp:3] -> [test.cpp:6]: (warning) Either the condition 'x<2' is redundant or the array 'a[3]' is accessed at index 3, which is out of bounds.\n", errout.str()); + + check("void f() {\n" // #2199 + " char a[5];\n" + " for (int i = 0; i < 5; i++) {\n" + " i += 8;\n" + " a[i] = 0;\n" + " }\n" + "}\n" + "void g() {\n" + " char a[5];\n" + " for (int i = 0; i < 5; i++) {\n" + " a[i + 7] = 0;\n" + " }\n" + "}\n"); + ASSERT_EQUALS( + "[test.cpp:5]: (error) Array 'a[5]' accessed at index 8, which is out of bounds.\n" + "[test.cpp:11]: (error) Array 'a[5]' accessed at index 11, which is out of bounds.\n", + errout.str()); } void array_index_59() // #10413 diff --git a/test/testcondition.cpp b/test/testcondition.cpp index d83c64980..b5d3f323e 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -5117,6 +5117,39 @@ private: " if (s.empty() || s.size() < 1) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition 's.size()<1' is always false\n", errout.str()); + + check("void bar(std::vector& vv) {\n" // #11464 + " class F {\n" + " public:\n" + " F(int, std::vector& lv) : mV(lv) {\n" + " mV.push_back(0);\n" + " }\n" + " private:\n" + " std::vector& mV;\n" + " } fi(1, vv);\n" + "}\n" + "void g() {\n" + " std::vector v;\n" + " bar(v);\n" + " if (v.empty()) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("struct F {\n" + " F(int, std::vector&lv) : mV(lv) {\n" + " mV.push_back(0);\n" + " }\n" + " std::vector& mV;\n" + "};\n" + "void g(std::vector& vv) {\n" + " F(1, vv);\n" + "}\n" + "void f() {\n" + " std::vector v;\n" + " g(v);\n" + " if (v.empty()) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void alwaysTrueLoop() diff --git a/test/testother.cpp b/test/testother.cpp index e73ef09e0..657450c3d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3451,6 +3451,24 @@ private: " g(U::V(&t));\n" "}\n"); ASSERT_EQUALS("[test.cpp:6]: (style) Parameter 't' can be declared as reference to const\n", errout.str()); + + check("void f1(std::vector& v) {\n" // #11207 + " auto it = v.cbegin();\n" + " while (it != v.cend()) {\n" + " if (*it > 12) {}\n" + " ++it;\n" + " }\n" + "}\n" + "void f2(std::vector& v) {\n" + " auto it = v.begin();\n" + " while (it != v.end()) {\n" + " if (*it > 12) {}\n" + " ++it;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n" + "[test.cpp:8]: (style) Parameter 'v' can be declared as reference to const\n", + errout.str()); } void constParameterCallback() {