TestNullPointer: Moved some std.cfg tests to the cfg testing

This commit is contained in:
Daniel Marjamäki 2015-01-28 16:22:45 +01:00
parent 7d0f5ad7c1
commit 70a38b1da6
2 changed files with 96 additions and 163 deletions

View File

@ -8,6 +8,7 @@
// //
#include <string.h> #include <string.h>
#include <stdio.h>
void strcpy_ok(char *a, char *b) { void strcpy_ok(char *a, char *b) {
strcpy(a,b); strcpy(a,b);
@ -20,3 +21,96 @@ void strcpy_bad() {
} }
// null pointer
void nullpointer(int value){
int res = 0;
FILE *fp;
// cppcheck-suppress nullPointer
clearerr(0);
// cppcheck-suppress nullPointer
feof(0);
// cppcheck-suppress nullPointer
fgetc(0);
// cppcheck-suppress nullPointer
fclose(0);
// cppcheck-suppress nullPointer
ferror(0);
// cppcheck-suppress nullPointer
ftell(0);
// cppcheck-suppress nullPointer
puts(0);
// cppcheck-suppress nullPointer
fp=fopen(0,0);
fclose(fp); fp = 0;
// No FP
fflush(0);
// No FP
fp = freopen(0,"abc",stdin);
fclose(fp); fp = 0;
// cppcheck-suppress nullPointer
fputc(0,0);
// cppcheck-suppress nullPointer
fputs(0,0);
// cppcheck-suppress nullPointer
fgetpos(0,0);
// cppcheck-suppress nullPointer
fsetpos(0,0);
// cppcheck-suppress nullPointer
itoa(123,0,10);
// cppcheck-suppress nullPointer
strchr(0,0);
// cppcheck-suppress nullPointer
strlen(0);
// cppcheck-suppress nullPointer
strcpy(0,0);
// cppcheck-suppress nullPointer
strspn(0,0);
// cppcheck-suppress nullPointer
strcspn(0,0);
// cppcheck-suppress nullPointer
strcoll(0,0);
// cppcheck-suppress nullPointer
strcat(0,0);
// cppcheck-suppress nullPointer
strcmp(0,0);
// cppcheck-suppress nullPointer
strncpy(0,0,1);
// cppcheck-suppress nullPointer
strncat(0,0,1);
// cppcheck-suppress nullPointer
strncmp(0,0,1);
// cppcheck-suppress nullPointer
strstr(0,0);
// cppcheck-suppress nullPointer
strtoul(0,0,0);
// cppcheck-suppress nullPointer
strtoull(0,0,0);
// cppcheck-suppress nullPointer
strtol(0,0,0);
// #6100 False positive nullPointer - calling mbstowcs(NULL,)
res += mbstowcs(0,value,0);
res += wcstombs(0,value,0);
strtok(NULL,"xyz");
strxfrm(0,"foo",0);
// TODO: error message
strxfrm(0,"foo",42);
}
void nullpointerMemchr1(char *p, char *s) {
p = memchr (s, 'p', strlen(s));
}
void nullpointerMemchr2(char *p, char *s) {
p = memchr (s, 0, strlen(s));
}
void nullpointerMemchr3(char *p) {
char *s = 0;
// cppcheck-suppress nullPointer
p = memchr (s, 0, strlen(s));
}

View File

@ -32,6 +32,8 @@ private:
Settings settings; Settings settings;
void run() { void run() {
// Load std.cfg library file
// TODO: This will be removed. The std.cfg is tested with the cfg testing.
LOAD_LIB_2(settings.library, "std.cfg"); LOAD_LIB_2(settings.library, "std.cfg");
TEST_CASE(nullpointerAfterLoop); TEST_CASE(nullpointerAfterLoop);
@ -83,12 +85,8 @@ private:
TEST_CASE(functioncallDefaultArguments); TEST_CASE(functioncallDefaultArguments);
TEST_CASE(nullpointer_internal_error); // #5080 TEST_CASE(nullpointer_internal_error); // #5080
TEST_CASE(nullpointerFputc); // #5645 FP: Null pointer dereference in fputc argument TEST_CASE(nullpointerFputc); // #5645 FP: Null pointer dereference in fputc argument
TEST_CASE(nullpointerMemchr);
TEST_CASE(nullpointerPutchar); TEST_CASE(nullpointerPutchar);
// Test that std.cfg is configured correctly
TEST_CASE(stdcfg);
// Load posix library file // Load posix library file
LOAD_LIB_2(settings.library, "posix.cfg"); LOAD_LIB_2(settings.library, "posix.cfg");
// Test that posix.cfg is configured correctly // Test that posix.cfg is configured correctly
@ -1780,31 +1778,6 @@ private:
// Test CheckNullPointer::nullConstantDereference // Test CheckNullPointer::nullConstantDereference
void nullConstantDereference() { void nullConstantDereference() {
// Ticket #2090
check("void foo() {\n"
" strcpy(0, \"abcd\");\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Null pointer dereference\n", errout.str());
// Ticket #1171
check("void foo(void* bar) {\n"
" if(strcmp(0, bar) == 0)\n"
" func();\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Null pointer dereference\n", errout.str());
// Ticket #2413 - it's ok to pass NULL to fflush
check("void foo() {\n"
" fflush(NULL);\n"
"}", true);
ASSERT_EQUALS("", errout.str());
// Ticket #3126 - don't confuse member function with standard function
check("void f() {\n"
" image1.fseek(0, SEEK_SET);\n"
"}", true);
ASSERT_EQUALS("", errout.str());
check("void f() {\n" check("void f() {\n"
" int* p = 0;\n" " int* p = 0;\n"
" return p[4];\n" " return p[4];\n"
@ -1812,18 +1785,10 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n" ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n"
"[test.cpp:3]: (error) Null pointer dereference\n", errout.str()); "[test.cpp:3]: (error) Null pointer dereference\n", errout.str());
check("void f(int x) {\n" // #4809 - passing "NULL"
" itoa(x,NULL,10);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Null pointer dereference\n", errout.str());
check("void f() {\n" check("void f() {\n"
" typeof(*NULL) y;\n" " typeof(*NULL) y;\n"
"}", true); "}", true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void f() { freopen(NULL, m, stdin); }");
ASSERT_EQUALS("", errout.str());
} }
void gcc_statement_expression() { void gcc_statement_expression() {
@ -2514,114 +2479,6 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
// Test that std.cfg is configured correctly
void stdcfg() {
const char errp[] = "[test.cpp:1] -> [test.cpp:1]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n";
const char errpq[] = "[test.cpp:1] -> [test.cpp:1]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n"
"[test.cpp:1] -> [test.cpp:1]: (warning) Possible null pointer dereference: q - otherwise it is redundant to check it against null.\n";
check("void f(FILE *p){ clearerr (p);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(FILE *p){ feof (p);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(FILE *p){ fgetc (p);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(FILE *p){ fclose (p);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(FILE *p){ ferror (p);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(FILE *p){ ftell (p);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(char *p){ puts (p);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(char * p,char * q){ fopen (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,FILE * q){ fputc (*p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,FILE * q){ fputs (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(FILE * p,fpos_t * q){ fgetpos (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(FILE * p,fpos_t * q){ fsetpos (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p){ strchr (p,c);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(char * p){ putchar (*p);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(char * p){ strlen (p);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(char * p,char * q){ strcpy (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,char * q){ strspn (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,char * q){ strcspn (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,char * q){ strcoll (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,char * q){ strcat (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,char * q){ strcmp (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,char * q){ strncpy (p,q,1);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,char * q){ strncat (p,q,1);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,char * q){ strncmp (p,q,1);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
check("void f(char * p,char * q){ strstr (p,q);if(!p||!q){}}");
ASSERT_EQUALS(errpq,errout.str());
// strtol etc
check("void f(char * p,char * q){ strtoul (p,q,0);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(char * p,char * q){ strtoull (p,q,0);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
check("void f(char * p,char * q){ strtol (p,q,0);if(!p){}}");
ASSERT_EQUALS(errp,errout.str());
// #6100 False positive nullPointer - calling mbstowcs(NULL,)
check("size_t get (char *value) { return mbstowcs (NULL, value, 0); }");
ASSERT_EQUALS("",errout.str());
check("size_t get (wchar_t *value) { return wcstombs (NULL, value, 0); }");
ASSERT_EQUALS("",errout.str());
check("void f() { strtok(NULL, 'x');}");
ASSERT_EQUALS("",errout.str());
// #6306 "false positive with strxfrm NULL argument"
check("void foo(void) { size_t res = strxfrm(NULL, \"foo\", 0); }");
ASSERT_EQUALS("",errout.str());
check("void foo(void) { size_t res = strxfrm(NULL, \"foo\", 42); }");
TODO_ASSERT_EQUALS("[test.cpp:1]: (error) Null pointer dereference\n", "", errout.str());
}
void nullpointerFputc() { void nullpointerFputc() {
check("int main () {\n" check("int main () {\n"
"FILE *fp = fopen(\"file.txt\", \"w+\");\n" "FILE *fp = fopen(\"file.txt\", \"w+\");\n"
@ -2641,24 +2498,6 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: nullstring\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: nullstring\n", errout.str());
} }
void nullpointerMemchr() {
check("void f (char *p, char *s) {\n"
" p = memchr (s, 'p', strlen(s));\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f (char *p, char *s) {\n"
" p = memchr (s, 0, strlen(s));\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f (char *p) {\n"
" char *s = 0;\n"
" p = memchr (s, 0, strlen(s));\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: s\n", errout.str());
}
void nullpointerPutchar() { void nullpointerPutchar() {
check("void f (char *c) {\n" check("void f (char *c) {\n"
" putchar(c);\n" " putchar(c);\n"