Skip to content

Commit 7c5db88

Browse files
Add tests for #10241, #11519, #12532, #13403 (danmar#8200)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 1e79f7f commit 7c5db88

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

test/testbufferoverrun.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4173,6 +4173,15 @@ class TestBufferOverrun : public TestFixture {
41734173
" a[i] = NULL;\n"
41744174
"}");
41754175
ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str());
4176+
4177+
check("void f(const uint8_t* a) {\n" // 10421
4178+
" uint8_t* p = (uint8_t*)malloc(20U * sizeof(uint8_t));\n"
4179+
" if (!p) return false;\n"
4180+
" for (uint8_t i = 1; i < 30; ++i)\n"
4181+
" p[i] = a[i];\n"
4182+
" free(p);\n"
4183+
"}\n");
4184+
ASSERT_EQUALS("[test.cpp:5:10]: (error) Array 'p[20]' accessed at index 29, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str());
41764185
}
41774186

41784187
// statically allocated buffer
@@ -5351,6 +5360,15 @@ class TestBufferOverrun : public TestFixture {
53515360
" f(a);\n"
53525361
"}\n");
53535362
ASSERT_EQUALS("[test.cpp:7:12] -> [test.cpp:9:6] -> [test.cpp:3:12]: (error) Array index out of bounds; 'p' buffer size is 4 and it is accessed at offset 20. [ctuArrayIndex]\n", errout_str());
5363+
5364+
ctu("void bar(int *p) { p[4] = 42; }\n" // #13403
5365+
"void f() {\n"
5366+
" int *p = (int*)malloc(4 * sizeof(int));\n"
5367+
" if (!p) return;\n"
5368+
" bar(p);\n"
5369+
" free(p);\n"
5370+
"}\n");
5371+
ASSERT_EQUALS("[test.cpp:3:12] -> [test.cpp:4:9] -> [test.cpp:5:8] -> [test.cpp:1:20]: (error) Array index out of bounds; 'p' buffer size is 16 and it is accessed at offset 16. [ctuArrayIndex]\n", errout_str());
53545372
}
53555373

53565374
void ctu_array() {

test/testcondition.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4815,6 +4815,29 @@ class TestCondition : public TestFixture {
48154815
" return false;\n"
48164816
"}\n");
48174817
ASSERT_EQUALS("[test.cpp:6:12] -> [test.cpp:7:21]: (style) Assigned value 's.g()' is always true [knownConditionTrueFalse]\n", errout_str());
4818+
4819+
check("void f(const void* p) {\n" // #11519
4820+
" bool b = false;\n"
4821+
" if (!p && !b) {}\n"
4822+
" if (!b) {}\n"
4823+
" (void)b;\n"
4824+
"}\n");
4825+
ASSERT_EQUALS("[test.cpp:3:15]: (style) Condition '!b' is always true [knownConditionTrueFalse]\n"
4826+
"[test.cpp:4:9]: (style) Condition '!b' is always true [knownConditionTrueFalse]\n",
4827+
errout_str());
4828+
4829+
check("struct C {\n" // #12532
4830+
" void f() const;\n"
4831+
" int a, b;\n"
4832+
"};\n"
4833+
"void C::f() const {\n"
4834+
" if (a)\n"
4835+
" return;\n"
4836+
" if (!b) {}\n"
4837+
" if (a) {}\n"
4838+
"}\n");
4839+
ASSERT_EQUALS("[test.cpp:6:9] -> [test.cpp:9:9]: (style) Condition 'a' is always false [knownConditionTrueFalse]\n",
4840+
errout_str());
48184841
}
48194842

48204843
void alwaysTrueSymbolic()

0 commit comments

Comments
 (0)