Skip to content

Commit cc0f613

Browse files
committed
more cleanup of std.cfg testing in TestBufferOverrun
1 parent 2da4303 commit cc0f613

File tree

1 file changed

+31
-76
lines changed

1 file changed

+31
-76
lines changed

test/testbufferoverrun.cpp

Lines changed: 31 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,6 @@ class TestBufferOverrun : public TestFixture {
8585
checkBufferOverrun.arrayIndexThenCheck();
8686
}
8787

88-
void checkstd(const char code[], const char filename[] = "test.cpp") {
89-
static bool init;
90-
static Settings settings;
91-
if (!init) {
92-
init = true;
93-
LOAD_LIB_2(settings.library, "std.cfg");
94-
settings.addEnabled("warning");
95-
}
96-
check(code, settings, filename);
97-
}
98-
9988
void run() {
10089
TEST_CASE(noerr1);
10190
TEST_CASE(noerr2);
@@ -208,23 +197,20 @@ class TestBufferOverrun : public TestFixture {
208197
TEST_CASE(strncat1);
209198
TEST_CASE(strncat2);
210199
TEST_CASE(strncat3);
211-
TEST_CASE(strncat4);
212200

213201
TEST_CASE(strcat1);
214202
TEST_CASE(strcat2);
215203
TEST_CASE(strcat3);
216204

217205
TEST_CASE(varid1);
218-
TEST_CASE(varid2);
219-
TEST_CASE(varid3); // ticket #4764
206+
TEST_CASE(varid2); // ticket #4764
220207

221208
TEST_CASE(assign1);
222209

223210
TEST_CASE(alloc_new); // Buffer allocated with new
224211
TEST_CASE(alloc_malloc); // Buffer allocated with malloc
225212
TEST_CASE(alloc_string); // statically allocated buffer
226213
TEST_CASE(alloc_alloca); // Buffer allocated with alloca
227-
TEST_CASE(malloc_memset); // using memset on buffer allocated with malloc
228214

229215
TEST_CASE(countSprintfLength);
230216
TEST_CASE(minsize_argvalue);
@@ -2727,65 +2713,56 @@ class TestBufferOverrun : public TestFixture {
27272713
}
27282714

27292715
void strncat1() {
2730-
checkstd("void f(char *a, char *b) {\n"
2731-
" char str[16];\n"
2732-
" strncpy(str, a, 10);\n"
2733-
" strncat(str, b, 10);\n"
2734-
"}");
2716+
check("void f(char *a, char *b) {\n"
2717+
" char str[16];\n"
2718+
" strncpy(str, a, 10);\n"
2719+
" strncat(str, b, 10);\n"
2720+
"}");
27352721
ASSERT_EQUALS("[test.cpp:4]: (warning) Dangerous usage of strncat - 3rd parameter is the maximum number of characters to append.\n", errout.str());
27362722
}
27372723

27382724
void strncat2() {
2739-
checkstd("void f(char *a) {\n"
2740-
" char str[5];\n"
2741-
" strncat(str, a, 5);\n"
2742-
"}");
2725+
check("void f(char *a) {\n"
2726+
" char str[5];\n"
2727+
" strncat(str, a, 5);\n"
2728+
"}");
27432729
ASSERT_EQUALS("[test.cpp:3]: (warning) Dangerous usage of strncat - 3rd parameter is the maximum number of characters to append.\n", errout.str());
27442730
}
27452731

27462732
void strncat3() {
2747-
checkstd("struct Foo { char a[4]; };\n"
2748-
"void f(char *a) {\n"
2749-
" struct Foo x;\n"
2750-
" strncat(x.a, a, 5);\n"
2751-
"}");
2752-
ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds: x.a\n", errout.str());
2753-
}
2754-
2755-
void strncat4() {
2756-
checkstd("void f(char *a) {\n"
2757-
" char str[5];\n"
2758-
" strncat(str, \"foobar\", 5);\n"
2759-
"}");
2733+
check("void f(char *a) {\n"
2734+
" char str[5];\n"
2735+
" strncat(str, \"foobar\", 5);\n"
2736+
"}");
27602737
ASSERT_EQUALS("[test.cpp:3]: (warning) Dangerous usage of strncat - 3rd parameter is the maximum number of characters to append.\n", errout.str());
27612738
}
27622739

27632740

27642741
void strcat1() {
2765-
checkstd("struct Foo { char a[4]; };\n"
2766-
"void f() {\n"
2767-
" struct Foo x;\n"
2768-
" strcat(x.a, \"aa\");\n"
2769-
" strcat(x.a, \"aa\");\n"
2770-
"}");
2742+
check("struct Foo { char a[4]; };\n"
2743+
"void f() {\n"
2744+
" struct Foo x;\n"
2745+
" strcat(x.a, \"aa\");\n"
2746+
" strcat(x.a, \"aa\");\n"
2747+
"}");
27712748
ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds.\n", errout.str());
27722749
}
27732750

27742751
void strcat2() {
2775-
checkstd("struct Foo { char a[5]; };\n"
2776-
"void f() {\n"
2777-
" struct Foo x;\n"
2778-
" strcat(x.a, \"aa\");\n"
2779-
" strcat(x.a, \"aa\");\n"
2780-
"}");
2752+
check("struct Foo { char a[5]; };\n"
2753+
"void f() {\n"
2754+
" struct Foo x;\n"
2755+
" strcat(x.a, \"aa\");\n"
2756+
" strcat(x.a, \"aa\");\n"
2757+
"}");
27812758
ASSERT_EQUALS("", errout.str());
27822759
}
27832760

27842761
void strcat3() {
2785-
checkstd("void f() {\n"
2786-
" INT str[10];\n"
2787-
" strcat(str, \"aa\");\n"
2788-
"}");
2762+
check("void f() {\n"
2763+
" INT str[10];\n"
2764+
" strcat(str, \"aa\");\n"
2765+
"}");
27892766
ASSERT_EQUALS("", errout.str());
27902767
}
27912768

@@ -2802,21 +2779,7 @@ class TestBufferOverrun : public TestFixture {
28022779
ASSERT_EQUALS("", errout.str());
28032780
}
28042781

2805-
2806-
void varid2() {
2807-
checkstd("void foo()\n"
2808-
"{\n"
2809-
" char str[10];\n"
2810-
" if (str[0])\n"
2811-
" {\n"
2812-
" char str[50];\n"
2813-
" memset(str,0,50);\n"
2814-
" }\n"
2815-
"}");
2816-
ASSERT_EQUALS("", errout.str());
2817-
}
2818-
2819-
void varid3() { // #4764
2782+
void varid2() { // #4764
28202783
check("struct foo {\n"
28212784
" void bar() { return; }\n"
28222785
" type<> member[1];\n"
@@ -2962,14 +2925,6 @@ class TestBufferOverrun : public TestFixture {
29622925
ASSERT_EQUALS("[test.cpp:4]: (error) Array 's[10]' accessed at index 10, which is out of bounds.\n", errout.str());
29632926
}
29642927

2965-
void malloc_memset() {
2966-
checkstd("void f() {\n"
2967-
" char *p = malloc(10);\n"
2968-
" memset(p,0,100);\n"
2969-
"}");
2970-
ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds.\n", errout.str());
2971-
}
2972-
29732928
void countSprintfLength() const {
29742929
std::list<const Token*> unknownParameter;
29752930
unknownParameter.push_back(0);

0 commit comments

Comments
 (0)