TestBufferOverrun: split up strncpy testing, keep checker testing in TestBufferOverrun and move cfg tests to cfg test

This commit is contained in:
Daniel Marjamäki 2015-02-09 16:20:28 +01:00
parent 38c8c3734d
commit 631536980c
2 changed files with 66 additions and 34 deletions

View File

@ -22,6 +22,17 @@ 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 // null pointer
void nullpointer(int value){ void nullpointer(int value){

View File

@ -21,6 +21,7 @@
#include "checkbufferoverrun.h" #include "checkbufferoverrun.h"
#include "testsuite.h" #include "testsuite.h"
#include <tinyxml2.h>
#include <sstream> #include <sstream>
#include <climits> #include <climits>
@ -70,15 +71,7 @@ private:
checkBufferOverrun.writeOutsideBufferSize(); checkBufferOverrun.writeOutsideBufferSize();
} }
void checkstd(const char code[], const char filename[] = "test.cpp") { void check(const char code[], const Settings &settings, 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");
}
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(&settings, this);
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, filename); tokenizer.tokenize(istr, filename);
@ -95,6 +88,17 @@ private:
checkBufferOverrun.writeOutsideBufferSize(); 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") { void checkposix(const char code[], const char filename[] = "test.cpp") {
static bool init; static bool init;
static Settings settings; static Settings settings;
@ -283,7 +287,7 @@ private:
TEST_CASE(memset1); TEST_CASE(memset1);
TEST_CASE(memset2); TEST_CASE(memset2);
TEST_CASE(counter_test); TEST_CASE(counter_test);
TEST_CASE(strncpy1); TEST_CASE(minsize_sizeof);
TEST_CASE(unknownType); TEST_CASE(unknownType);
TEST_CASE(terminateStrncpy1); TEST_CASE(terminateStrncpy1);
@ -3740,35 +3744,52 @@ private:
} }
void strncpy1() { void minsize_sizeof() {
checkstd("void f() {\n" Settings settings;
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"strncpy\">\n"
" <noreturn>false</noreturn>\n"
" <arg nr=\"1\">\n"
" <minsize type=\"sizeof\" arg=\"2\"/>\n"
" <minsize type=\"argvalue\" arg=\"3\"/>\n"
" </arg>\n"
" <arg nr=\"2\"/>\n"
" <arg nr=\"3\"/>\n"
" </function>\n"
"</def>";
tinyxml2::XMLDocument doc;
doc.Parse(xmldata, sizeof(xmldata));
settings.library.load(doc);
check("void f() {\n"
" char c[7];\n" " char c[7];\n"
" strncpy(c, \"hello\", 7);\n" " strncpy(c, \"hello\", 7);\n"
"}"); "}", settings);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkstd("void f() {\n" check("void f() {\n"
" char c[6];\n" " char c[6];\n"
" strncpy(c,\"hello\",6);\n" " strncpy(c,\"hello\",6);\n"
"}"); "}", settings);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkstd("void f() {\n" check("void f() {\n"
" char c[5];\n" " char c[5];\n"
" strncpy(c,\"hello\",6);\n" " strncpy(c,\"hello\",6);\n"
"}"); "}", settings);
ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout.str());
checkstd("void f() {\n" check("void f() {\n"
" char c[6];\n" " char c[6];\n"
" strncpy(c,\"hello!\",7);\n" " strncpy(c,\"hello!\",7);\n"
"}"); "}", settings);
ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout.str());
checkstd("struct AB { char a[10]; };\n" check("struct AB { char a[10]; };\n"
"void foo(AB *ab) {\n" "void foo(AB *ab) {\n"
" strncpy(x, ab->a, 100);\n" " strncpy(x, ab->a, 100);\n"
"}"); "}", settings);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }