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,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) {

View File

@ -21,6 +21,7 @@
#include "checkbufferoverrun.h"
#include "testsuite.h"
#include <tinyxml2.h>
#include <sstream>
#include <climits>
@ -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[] = "<?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"
" 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());
}