Skip to content

Commit 52253a5

Browse files
committed
[NFC] Add testcase requested and organize test cases
1 parent 8f99d67 commit 52253a5

1 file changed

Lines changed: 48 additions & 41 deletions

File tree

test/testbufferoverrun.cpp

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class TestBufferOverrun : public TestFixture {
231231
TEST_CASE(buffer_overrun_function_array_argument);
232232
TEST_CASE(possible_buffer_overrun_1); // #3035
233233
TEST_CASE(buffer_overrun_readSizeFromCfg);
234+
TEST_CASE(buffer_overrun_handle_addr_of_var); // ticket #7570 - correctly handle &var
234235

235236
TEST_CASE(valueflow_string); // using ValueFlow string values in checking
236237

@@ -270,6 +271,7 @@ class TestBufferOverrun : public TestFixture {
270271
TEST_CASE(terminateStrncpy3);
271272
TEST_CASE(terminateStrncpy4);
272273
TEST_CASE(terminateStrncpy5); // #9944
274+
TEST_CASE(terminateStrncpy_handle_addr_of_var); // #7570
273275
TEST_CASE(recursive_long_time);
274276

275277
TEST_CASE(crash1); // Ticket #1587 - crash
@@ -306,8 +308,6 @@ class TestBufferOverrun : public TestFixture {
306308
TEST_CASE(objectIndex);
307309

308310
TEST_CASE(checkPipeParameterSize); // ticket #3521
309-
310-
TEST_CASE(getBufferSizeOfAddressOfVariable); // ticket #7570
311311
}
312312

313313

@@ -3725,6 +3725,38 @@ class TestBufferOverrun : public TestFixture {
37253725
ASSERT_EQUALS("[test.cpp:3:16]: (error) Buffer is accessed out of bounds: ms.str [bufferAccessOutOfBounds]\n", errout_str());
37263726
}
37273727

3728+
void buffer_overrun_handle_addr_of_var() { // #7570
3729+
check("void f() {\n"
3730+
" int i;\n"
3731+
" memset(i, 0, 1000);\n"
3732+
"}");
3733+
ASSERT_EQUALS("", errout_str());
3734+
3735+
check("void f() {\n"
3736+
" int i;\n"
3737+
" memset(&i, 0, 1000);\n"
3738+
"}");
3739+
ASSERT_EQUALS("[test.cpp:3:10]: (error) Buffer is accessed out of bounds: &i [bufferAccessOutOfBounds]\n", errout_str());
3740+
3741+
check("void f() {\n"
3742+
" int i[2];\n"
3743+
" memset(&i, 0, 1000);\n"
3744+
"}");
3745+
ASSERT_EQUALS("[test.cpp:3:10]: (error) Buffer is accessed out of bounds: i [bufferAccessOutOfBounds]\n", errout_str());
3746+
3747+
check("void f() {\n"
3748+
" int i;\n"
3749+
" memset(&i, 0, sizeof(i));\n"
3750+
"}");
3751+
ASSERT_EQUALS("", errout_str());
3752+
3753+
check("void f() {\n"
3754+
" int i[10];\n"
3755+
" memset(&i[1], 0, 1000);\n"
3756+
"}");
3757+
TODO_ASSERT_EQUALS("[test.cpp:3:10]: (error) Buffer is accessed out of bounds: &i[1] [bufferAccessOutOfBounds]\n", "", errout_str());
3758+
}
3759+
37283760
void valueflow_string() { // using ValueFlow string values in checking
37293761
check("char f() {\n"
37303762
" const char *x = s;\n"
@@ -4686,6 +4718,20 @@ class TestBufferOverrun : public TestFixture {
46864718
}
46874719
// extracttests.enable
46884720

4721+
void terminateStrncpy_handle_addr_of_var() { // #7570
4722+
check("void foo() {\n"
4723+
" char c[6];\n"
4724+
" strncpy(&c, \"hello!\", 6);\n"
4725+
"}");
4726+
ASSERT_EQUALS("[test.cpp:3:3]: (warning, inconclusive) The buffer 'c' may not be null-terminated after the call to strncpy(). [terminateStrncpy]\n", errout_str());
4727+
4728+
check("void foo() {\n"
4729+
" char c[6];\n"
4730+
" strncpy(&c, \"hello\\0\", 6);\n"
4731+
"}");
4732+
ASSERT_EQUALS("", errout_str());
4733+
}
4734+
46894735
void recursive_long_time() {
46904736
// Just test that recursive check doesn't take long time
46914737
check("char *f2 ( char *b )\n"
@@ -5714,45 +5760,6 @@ class TestBufferOverrun : public TestFixture {
57145760
"}", dinit(CheckOptions, $.s = &settings));
57155761
ASSERT_EQUALS("", errout_str());
57165762
}
5717-
5718-
void getBufferSizeOfAddressOfVariable() { // #7570
5719-
5720-
check("void f() {\n"
5721-
" int i;\n"
5722-
" memset(&i, 0, 1000);\n"
5723-
"}");
5724-
ASSERT_EQUALS("[test.cpp:3:10]: (error) Buffer is accessed out of bounds: &i [bufferAccessOutOfBounds]\n", errout_str());
5725-
5726-
check("void f() {\n"
5727-
" int i[2];\n"
5728-
" memset(&i, 0, 1000);\n"
5729-
"}");
5730-
ASSERT_EQUALS("[test.cpp:3:10]: (error) Buffer is accessed out of bounds: i [bufferAccessOutOfBounds]\n", errout_str());
5731-
5732-
check("void f() {\n"
5733-
" int i;\n"
5734-
" memset(&i, 0, sizeof(i));\n"
5735-
"}");
5736-
ASSERT_EQUALS("", errout_str());
5737-
5738-
check("void f() {\n"
5739-
" int i[10];\n"
5740-
" memset(&i[1], 0, 1000);\n"
5741-
"}");
5742-
TODO_ASSERT_EQUALS("[test.cpp:3:10]: (error) Buffer is accessed out of bounds: &i[1] [bufferAccessOutOfBounds]\n", "", errout_str());
5743-
5744-
check("void foo() {\n"
5745-
" char c[6];\n"
5746-
" strncpy(&c, \"hello!\", 6);\n"
5747-
"}");
5748-
ASSERT_EQUALS("[test.cpp:3:3]: (warning, inconclusive) The buffer 'c' may not be null-terminated after the call to strncpy(). [terminateStrncpy]\n", errout_str());
5749-
5750-
check("void foo() {\n"
5751-
" char c[6];\n"
5752-
" strncpy(&c, \"hello\\0\", 6);\n"
5753-
"}");
5754-
ASSERT_EQUALS("", errout_str());
5755-
}
57565763
};
57575764

57585765
REGISTER_TEST(TestBufferOverrun)

0 commit comments

Comments
 (0)