TestBufferOverrun: Moved out cfg tests. Created minsize-mul testing in TestBufferOverrun

This commit is contained in:
Daniel Marjamäki 2015-02-10 06:44:19 +01:00
parent ce1d0099cf
commit 9aa0420472
2 changed files with 37 additions and 27 deletions

View File

@ -22,7 +22,12 @@ void bufferAccessOutOf(void) {
strncpy(a,"abcde",5);
// cppcheck-suppress bufferAccessOutOfBounds
strncpy(a,"abcde",6);
fread(a,1,5,stdin);
// cppcheck-suppress bufferAccessOutOfBounds
fread(a,1,6,stdin);
fwrite(a,1,5,stdout);
// cppcheck-suppress bufferAccessOutOfBounds
fread(a,1,6,stdout);
}
// null pointer

View File

@ -289,6 +289,7 @@ private:
TEST_CASE(counter_test);
TEST_CASE(minsize_argvalue);
TEST_CASE(minsize_sizeof);
TEST_CASE(minsize_mul);
TEST_CASE(unknownType);
TEST_CASE(terminateStrncpy1);
@ -2226,32 +2227,6 @@ private:
void buffer_overrun_1_standard_functions() {
// fread
checkstd("void f(FILE* fd) {\n"
" char str[3];\n"
" fread(str,1,4,fd);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: str\n", errout.str());
checkstd("void f(FILE* fd) {\n"
" char str[3];\n"
" fread(str,1,3,fd);\n"
"}");
ASSERT_EQUALS("", errout.str());
// fwrite
checkstd("void f(FILE* fd) {\n"
" char str[3];\n"
" fwrite(str,1,4,fd);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: str\n", errout.str());
checkstd("void f(FILE* fd) {\n"
" char str[3];\n"
" fwrite(str,1,3,fd);\n"
"}");
ASSERT_EQUALS("", errout.str());
// #4968 - not standard function
checkstd("void f() {\n"
" char str[3];\n"
@ -3806,6 +3781,36 @@ private:
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (error) Buffer is accessed out of bounds: buf\n", errout.str());
}
void minsize_mul() {
Settings settings;
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"fread\">\n"
" <arg nr=\"1\">\n"
" <minsize type=\"mul\" arg=\"2\" arg2=\"3\"/>\n"
" </arg>\n"
" <arg nr=\"2\"/>\n"
" <arg nr=\"3\"/>\n"
" <arg nr=\"4\"/>\n"
" </function>\n"
"</def>";
tinyxml2::XMLDocument doc;
doc.Parse(xmldata, sizeof(xmldata));
settings.library.load(doc);
check("void f() {\n"
" char c[5];\n"
" fread(c, 1, 5, stdin);\n"
"}", settings);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" char c[5];\n"
" fread(c, 1, 6, stdin);\n"
"}", settings);
ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: c\n", errout.str());
}
void unknownType() {
check("void f()\n"
"{\n"