Skip to content

Commit

Permalink
Add tests for danmar#2199, #11207, #11464 (danmar#5817)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Jan 2, 2024
1 parent 98b9244 commit 14627ca
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,24 @@ class TestBufferOverrun : public TestFixture {
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
Expand Down
33 changes: 33 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5117,6 +5117,39 @@ class TestCondition : public TestFixture {
" 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<int>& vv) {\n" // #11464
" class F {\n"
" public:\n"
" F(int, std::vector<int>& lv) : mV(lv) {\n"
" mV.push_back(0);\n"
" }\n"
" private:\n"
" std::vector<int>& mV;\n"
" } fi(1, vv);\n"
"}\n"
"void g() {\n"
" std::vector<int> v;\n"
" bar(v);\n"
" if (v.empty()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("struct F {\n"
" F(int, std::vector<int>&lv) : mV(lv) {\n"
" mV.push_back(0);\n"
" }\n"
" std::vector<int>& mV;\n"
"};\n"
"void g(std::vector<int>& vv) {\n"
" F(1, vv);\n"
"}\n"
"void f() {\n"
" std::vector<int> v;\n"
" g(v);\n"
" if (v.empty()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void alwaysTrueLoop()
Expand Down
18 changes: 18 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3451,6 +3451,24 @@ class TestOther : public TestFixture {
" 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<int>& 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<int>& 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() {
Expand Down

0 comments on commit 14627ca

Please sign in to comment.