Skip to content

Commit bf89a6f

Browse files
Add tests for cppcheck-opensource#6092, #9995, #13766 (cppcheck-opensource#9007)
1 parent 23271b4 commit bf89a6f

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎test/testbufferoverrun.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ class TestBufferOverrun : public TestFixture {
164164
TEST_CASE(array_index_74); // #11088
165165
TEST_CASE(array_index_75);
166166
TEST_CASE(array_index_76);
167+
TEST_CASE(array_index_77);
167168
TEST_CASE(array_index_multidim);
168169
TEST_CASE(array_index_switch_in_for);
169170
TEST_CASE(array_index_for_in_for); // FP: #2634
@@ -2014,6 +2015,21 @@ class TestBufferOverrun : public TestFixture {
20142015
errout_str());
20152016
}
20162017

2018+
void array_index_77()
2019+
{
2020+
check("int f(bool b) {\n" // #6092
2021+
" const int a[2] = {};\n"
2022+
" int i = 0, s = 0;\n"
2023+
" s += a[i++];\n"
2024+
" if (b)\n"
2025+
" s += a[i++];\n"
2026+
" s += a[i++];\n"
2027+
" return s + i;\n"
2028+
"}\n");
2029+
ASSERT_EQUALS("[test.cpp:7:11]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n",
2030+
errout_str());
2031+
}
2032+
20172033
void array_index_multidim() {
20182034
check("void f()\n"
20192035
"{\n"

‎test/testother.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,11 @@ class TestOther : public TestFixture {
23672367
" return N::f(float_ptr(b.data()));\n" // <- the cast is safe
23682368
"}\n");
23692369
ASSERT_EQUALS("[test.cpp:9:17]: (style) C-style pointer casting [cstyleCast]\n", errout_str());
2370+
2371+
checkOldStylePointerCast("void f(const T& x) {\n" // #13766
2372+
" const U& y = (typename const U&)(x);\n"
2373+
"}\n");
2374+
ASSERT_EQUALS("[test.cpp:2:18]: (warning) Potentially invalid type conversion in old-style C cast, clarify/fix with C++ cast [dangerousTypeCast]\n", errout_str());
23702375
}
23712376

23722377
#define checkIntToPointerCast(...) checkIntToPointerCast_(__FILE__, __LINE__, __VA_ARGS__)

‎test/testunusedvar.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5688,6 +5688,20 @@ class TestUnusedVar : public TestFixture {
56885688
" a[0].b = 0;\n"
56895689
"}\n");
56905690
ASSERT_EQUALS("", errout_str());
5691+
5692+
functionVariableUsage("struct S {\n" // #9995
5693+
" int& operator[](int i) { return r[i]; }\n"
5694+
" std::map<int, int>& r;\n"
5695+
"};\n"
5696+
"struct T {\n"
5697+
" std::map<int, int>& m;\n"
5698+
" S get() { return { m }; }\n"
5699+
"};\n"
5700+
"void f(T& t) {\n"
5701+
" S s = t.get();\n"
5702+
" s[1] = 0;\n"
5703+
"}\n");
5704+
ASSERT_EQUALS("", errout_str());
56915705
}
56925706

56935707
void localvarUnion1() {

0 commit comments

Comments
 (0)