From 631536980c2591a005ccd8a2e9bbff73c316794f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 9 Feb 2015 16:20:28 +0100 Subject: [PATCH] TestBufferOverrun: split up strncpy testing, keep checker testing in TestBufferOverrun and move cfg tests to cfg test --- test/cfg/std.c | 17 ++++++-- test/testbufferoverrun.cpp | 83 ++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 34 deletions(-) diff --git a/test/cfg/std.c b/test/cfg/std.c index 4fb3e7911..f41449521 100644 --- a/test/cfg/std.c +++ b/test/cfg/std.c @@ -22,12 +22,23 @@ void strcpy_bad() { } +void strncpy_ok() { + char a[5]; + strncpy(a,"hello world!",5); +} + +void strncpy_bad() { + char a[5]; + // cppcheck-suppress bufferAccessOutOfBounds + strncpy(a, "hello world!",10); +} + // null pointer void nullpointer(int value){ int res = 0; FILE *fp; - + // cppcheck-suppress nullPointer clearerr(0); // cppcheck-suppress nullPointer @@ -184,7 +195,7 @@ void uninit_fgetpos(void) { fp = fopen("filename","rt"); // cppcheck-suppress uninitvar fgetpos(fp,ppos); - fclose(fp); + fclose(fp); } void uninit_fsetpos(void) { @@ -197,7 +208,7 @@ void uninit_fsetpos(void) { fp = fopen("filename","rt"); // cppcheck-suppress uninitvar fsetpos(fp,ppos); - fclose(fp); + fclose(fp); } void uninit_fgets(void) { diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index aacea9c0a..d528c49d3 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -21,6 +21,7 @@ #include "checkbufferoverrun.h" #include "testsuite.h" +#include #include #include @@ -70,15 +71,7 @@ private: checkBufferOverrun.writeOutsideBufferSize(); } - void checkstd(const char code[], const char filename[] = "test.cpp") { - static bool init; - static Settings settings; - if (!init) { - init = true; - LOAD_LIB_2(settings.library, "std.cfg"); - settings.addEnabled("warning"); - } - + void check(const char code[], const Settings &settings, const char filename[] = "test.cpp") { Tokenizer tokenizer(&settings, this); std::istringstream istr(code); tokenizer.tokenize(istr, filename); @@ -95,6 +88,17 @@ private: checkBufferOverrun.writeOutsideBufferSize(); } + void checkstd(const char code[], const char filename[] = "test.cpp") { + static bool init; + static Settings settings; + if (!init) { + init = true; + LOAD_LIB_2(settings.library, "std.cfg"); + settings.addEnabled("warning"); + } + check(code, settings, filename); + } + void checkposix(const char code[], const char filename[] = "test.cpp") { static bool init; static Settings settings; @@ -283,7 +287,7 @@ private: TEST_CASE(memset1); TEST_CASE(memset2); TEST_CASE(counter_test); - TEST_CASE(strncpy1); + TEST_CASE(minsize_sizeof); TEST_CASE(unknownType); TEST_CASE(terminateStrncpy1); @@ -3740,35 +3744,52 @@ private: } - void strncpy1() { - checkstd("void f() {\n" - " char c[7];\n" - " strncpy(c, \"hello\", 7);\n" - "}"); + void minsize_sizeof() { + Settings settings; + const char xmldata[] = "\n" + "\n" + " \n" + " false\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + ""; + tinyxml2::XMLDocument doc; + doc.Parse(xmldata, sizeof(xmldata)); + settings.library.load(doc); + + check("void f() {\n" + " char c[7];\n" + " strncpy(c, \"hello\", 7);\n" + "}", settings); ASSERT_EQUALS("", errout.str()); - checkstd("void f() {\n" - " char c[6];\n" - " strncpy(c,\"hello\",6);\n" - "}"); + check("void f() {\n" + " char c[6];\n" + " strncpy(c,\"hello\",6);\n" + "}", settings); ASSERT_EQUALS("", errout.str()); - checkstd("void f() {\n" - " char c[5];\n" - " strncpy(c,\"hello\",6);\n" - "}"); + check("void f() {\n" + " char c[5];\n" + " strncpy(c,\"hello\",6);\n" + "}", settings); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout.str()); - checkstd("void f() {\n" - " char c[6];\n" - " strncpy(c,\"hello!\",7);\n" - "}"); + check("void f() {\n" + " char c[6];\n" + " strncpy(c,\"hello!\",7);\n" + "}", settings); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout.str()); - checkstd("struct AB { char a[10]; };\n" - "void foo(AB *ab) {\n" - " strncpy(x, ab->a, 100);\n" - "}"); + check("struct AB { char a[10]; };\n" + "void foo(AB *ab) {\n" + " strncpy(x, ab->a, 100);\n" + "}", settings); ASSERT_EQUALS("", errout.str()); }