From 851e81cd12cfa31d6a5099a4c0fea6233237bdf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 16 Feb 2008 16:45:56 +0000 Subject: [PATCH] Unit Testing: All tests for buffer overruns were moved --- testbufferoverrun1/err.msg | 1 - testbufferoverrun1/testbufferoverrun1.cpp | 8 -- testbufferoverrun2/err.msg | 1 - testbufferoverrun2/testbufferoverrun2.cpp | 8 -- testbufferoverrun3/err.msg | 0 testbufferoverrun3/testbufferoverrun3.cpp | 13 --- testbufferoverrun4/err.msg | 1 - testbufferoverrun4/testbufferoverrun4.cpp | 7 -- testbufferoverrun5/err.msg | 1 - testbufferoverrun5/testbufferoverrun5.cpp | 11 -- testbufferoverrun6/err.msg | 1 - testbufferoverrun6/testbufferoverrun6.cpp | 10 -- testbufferoverrun7/err.msg | 4 - testbufferoverrun7/testbufferoverrun7.cpp | 21 ---- tests.cpp | 134 +++++++++++++++++++--- 15 files changed, 119 insertions(+), 102 deletions(-) delete mode 100644 testbufferoverrun1/err.msg delete mode 100644 testbufferoverrun1/testbufferoverrun1.cpp delete mode 100644 testbufferoverrun2/err.msg delete mode 100644 testbufferoverrun2/testbufferoverrun2.cpp delete mode 100644 testbufferoverrun3/err.msg delete mode 100644 testbufferoverrun3/testbufferoverrun3.cpp delete mode 100644 testbufferoverrun4/err.msg delete mode 100644 testbufferoverrun4/testbufferoverrun4.cpp delete mode 100644 testbufferoverrun5/err.msg delete mode 100644 testbufferoverrun5/testbufferoverrun5.cpp delete mode 100644 testbufferoverrun6/err.msg delete mode 100644 testbufferoverrun6/testbufferoverrun6.cpp delete mode 100644 testbufferoverrun7/err.msg delete mode 100644 testbufferoverrun7/testbufferoverrun7.cpp diff --git a/testbufferoverrun1/err.msg b/testbufferoverrun1/err.msg deleted file mode 100644 index 1d051bcd3..000000000 --- a/testbufferoverrun1/err.msg +++ /dev/null @@ -1 +0,0 @@ -[testbufferoverrun1\testbufferoverrun1.cpp:7]: Array index out of bounds diff --git a/testbufferoverrun1/testbufferoverrun1.cpp b/testbufferoverrun1/testbufferoverrun1.cpp deleted file mode 100644 index f4c57d375..000000000 --- a/testbufferoverrun1/testbufferoverrun1.cpp +++ /dev/null @@ -1,8 +0,0 @@ - - -void f() -{ - char str[0x10]; - str[15] = 0; - str[16] = 0; -} \ No newline at end of file diff --git a/testbufferoverrun2/err.msg b/testbufferoverrun2/err.msg deleted file mode 100644 index fa65c7570..000000000 --- a/testbufferoverrun2/err.msg +++ /dev/null @@ -1 +0,0 @@ -[testbufferoverrun2\testbufferoverrun2.cpp:7]: Buffer overrun diff --git a/testbufferoverrun2/testbufferoverrun2.cpp b/testbufferoverrun2/testbufferoverrun2.cpp deleted file mode 100644 index e5d61d4d8..000000000 --- a/testbufferoverrun2/testbufferoverrun2.cpp +++ /dev/null @@ -1,8 +0,0 @@ - - -void f() -{ - int val[50]; - for (i = 0; i < 100; i++) - sum += val[i]; -} \ No newline at end of file diff --git a/testbufferoverrun3/err.msg b/testbufferoverrun3/err.msg deleted file mode 100644 index e69de29bb..000000000 diff --git a/testbufferoverrun3/testbufferoverrun3.cpp b/testbufferoverrun3/testbufferoverrun3.cpp deleted file mode 100644 index c677e3338..000000000 --- a/testbufferoverrun3/testbufferoverrun3.cpp +++ /dev/null @@ -1,13 +0,0 @@ - - -void f() -{ - if (ab) - { - char str[50]; - } - if (ab) - { - char str[50]; - } -} \ No newline at end of file diff --git a/testbufferoverrun4/err.msg b/testbufferoverrun4/err.msg deleted file mode 100644 index 8bdd46a06..000000000 --- a/testbufferoverrun4/err.msg +++ /dev/null @@ -1 +0,0 @@ -[testbufferoverrun4\testbufferoverrun4.cpp:6]: Buffer overrun diff --git a/testbufferoverrun4/testbufferoverrun4.cpp b/testbufferoverrun4/testbufferoverrun4.cpp deleted file mode 100644 index bccb3e09a..000000000 --- a/testbufferoverrun4/testbufferoverrun4.cpp +++ /dev/null @@ -1,7 +0,0 @@ - - -void f() -{ - char str[3]; - strcpy(str, "abc"); -} diff --git a/testbufferoverrun5/err.msg b/testbufferoverrun5/err.msg deleted file mode 100644 index f0ddcb231..000000000 --- a/testbufferoverrun5/err.msg +++ /dev/null @@ -1 +0,0 @@ -[testbufferoverrun5\testbufferoverrun5.cpp:8]: Array index out of bounds diff --git a/testbufferoverrun5/testbufferoverrun5.cpp b/testbufferoverrun5/testbufferoverrun5.cpp deleted file mode 100644 index 8cd4bdb05..000000000 --- a/testbufferoverrun5/testbufferoverrun5.cpp +++ /dev/null @@ -1,11 +0,0 @@ - - -const int SIZE = 10; - -void f() -{ - int i[SIZE]; - i[SIZE] = 0; -} - - diff --git a/testbufferoverrun6/err.msg b/testbufferoverrun6/err.msg deleted file mode 100644 index 4e67a2dda..000000000 --- a/testbufferoverrun6/err.msg +++ /dev/null @@ -1 +0,0 @@ -[testbufferoverrun6\testbufferoverrun6.cpp:8]: Array index out of bounds diff --git a/testbufferoverrun6/testbufferoverrun6.cpp b/testbufferoverrun6/testbufferoverrun6.cpp deleted file mode 100644 index 81d7a2d97..000000000 --- a/testbufferoverrun6/testbufferoverrun6.cpp +++ /dev/null @@ -1,10 +0,0 @@ - - - -void f() -{ - int i[10]; - - i[ sizeof(i) - 1 ] = 0; - -} \ No newline at end of file diff --git a/testbufferoverrun7/err.msg b/testbufferoverrun7/err.msg deleted file mode 100644 index 86cac18ef..000000000 --- a/testbufferoverrun7/err.msg +++ /dev/null @@ -1,4 +0,0 @@ -[testbufferoverrun7\testbufferoverrun7.cpp:5]: A string with unknown length is copied to buffer. -[testbufferoverrun7\testbufferoverrun7.cpp:10]: A string with unknown length is copied to buffer. -[testbufferoverrun7\testbufferoverrun7.cpp:15]: A string with unknown length is copied to buffer. -[testbufferoverrun7\testbufferoverrun7.cpp:20]: A string with unknown length is copied to buffer. diff --git a/testbufferoverrun7/testbufferoverrun7.cpp b/testbufferoverrun7/testbufferoverrun7.cpp deleted file mode 100644 index 1482d3a0d..000000000 --- a/testbufferoverrun7/testbufferoverrun7.cpp +++ /dev/null @@ -1,21 +0,0 @@ - - -void f1(char *str) -{ - strcpy(buf,str); -} - -void f2(char *str) -{ - strcat(buf,str); -} - -void f3(char *str) -{ - sprintf(buf,"%s",str); -} - -void f4(const char str[]) -{ - strcpy(buf, str); -} diff --git a/tests.cpp b/tests.cpp index 1d6771690..307df0a31 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1,6 +1,8 @@ // Unit-testing cppcheck +//--------------------------------------------------------------------------- + #include "tokenize.h" // <- Tokenizer #include "CommonCheck.h" #include "CheckBufferOverrun.h" @@ -8,20 +10,23 @@ #include #include -#define assert_equal(A,B) if (A!=B) { std::cerr << "Failed at line " << __LINE__ << "\n"; FailCount++; } else { SuccessCount++; } +//--------------------------------------------------------------------------- +#define assert_equal(A,B) if (A!=B) { std::cerr << "Failed at line " << line << "\n"; FailCount++; } else { SuccessCount++; } +//--------------------------------------------------------------------------- bool Debug = false; bool ShowAll = false; bool CheckCodingStyle = false; - +//--------------------------------------------------------------------------- static unsigned int FailCount, SuccessCount; - -static void buffer_overrun_1(); +//--------------------------------------------------------------------------- +static void buffer_overrun(); +//--------------------------------------------------------------------------- int main() { Files.push_back( std::string("test.cpp") ); - buffer_overrun_1(); + buffer_overrun(); std::cout << "Success Rate: " << SuccessCount << " / " @@ -29,28 +34,127 @@ int main() << std::endl; return 0; } +//--------------------------------------------------------------------------- -static void buffer_overrun_1() +static void buffer_overrun_check(const unsigned int line, + const char code[], + const char msg[]) { - const char code[] = "void f()\n" - "{\n" - " char str[0x10];\n" - " str[15] = 0;\n" - " str[16] = 0;\n" - "}\n"; - // Tokenize.. tokens = tokens_back = NULL; std::istringstream istr(code); TokenizeCode( istr ); SimplifyTokenList(); + // Check for buffer overruns.. errout.str(""); CheckBufferOverrun(); - std::string err = errout.str(); - assert_equal( errout.str(), "[test.cpp:5]: Array index out of bounds\n" ); + // Check the error messages.. + assert_equal(errout.str(), msg); + + // Cleanup.. + DeallocateTokens(); } +//--------------------------------------------------------------------------- + +static void buffer_overrun() +{ + // test1: numeric array index + // test2: variable array index (for-loop) + // test3: creating several arrays with the same names. + // test4: using strcpy -> check string length + // test5: constant array index + // test6: calculated array index that is out of bounds + + const char test1[] = "void f()\n" + "{\n" + " char str[0x10];\n" + " str[15] = 0;\n" + " str[16] = 0;\n" + "}\n"; + buffer_overrun_check( __LINE__, test1, "[test.cpp:5]: Array index out of bounds\n" ); + const char test2[] = "void f()\n" + "{\n" + " int val[50];\n" + " for (i = 0; i < 100; i++)\n" + " sum += val[i];\n" + "}\n"; + buffer_overrun_check( __LINE__, test2, "[test.cpp:5]: Buffer overrun\n" ); + + + + const char test3[] = "void f()\n" + "{\n" + " if (ab)\n" + " {\n" + " char str[50];\n" + " }\n" + " if (ab)\n" + " {\n" + " char str[50];\n" + " }\n" + "}\n"; + buffer_overrun_check( __LINE__, test3, "" ); + + + + const char test4[] = "void f()\n" + "{\n" + " char str[3];\n" + " strcpy(str, \"abc\");\n" + "}\n"; + buffer_overrun_check( __LINE__, test4, "[test.cpp:4]: Buffer overrun\n" ); + + + + const char test5[] = "const int SIZE = 10;\n" + "void f()\n" + "{\n" + " int i[SIZE];\n" + " i[SIZE] = 0;\n" + "}\n"; + buffer_overrun_check( __LINE__, test5, "[test.cpp:5]: Array index out of bounds\n" ); + + + + + const char test6[] = "void f()\n" + "{\n" + " int i[10];\n" + " i[ sizeof(i) - 1 ] = 0;\n" + "}\n"; + buffer_overrun_check( __LINE__, test6, "[test.cpp:4]: Array index out of bounds\n" ); + + + + + const char test7[] = "void f1(char *str)\n" + "{\n" + " strcpy(buf,str);\n" + "}\n" + "void f2(char *str)\n" + "{\n" + " strcat(buf,str);\n" + "}\n" + "void f3(char *str)\n" + "{\n" + " sprintf(buf,\"%s\",str);\n" + "}\n" + "void f4(const char str[])\n" + "{\n" + " strcpy(buf, str);\n" + "}\n"; + const char err7[] = + "[test.cpp:3]: A string with unknown length is copied to buffer.\n" + "[test.cpp:7]: A string with unknown length is copied to buffer.\n" + "[test.cpp:11]: A string with unknown length is copied to buffer.\n" + "[test.cpp:15]: A string with unknown length is copied to buffer.\n"; + + buffer_overrun_check( __LINE__, test7, err7 ); + +} +//---------------------------------------------------------------------------