From fe1b46c17476455522cb8757866ba729617aa714 Mon Sep 17 00:00:00 2001 From: Martin Ettl Date: Wed, 19 Mar 2014 21:08:48 +0100 Subject: [PATCH] Extended std.cfg --- cfg/std.cfg | 24 ++++-- cli/cppcheckexecutor.cpp | 2 +- test/testnullpointer.cpp | 57 +++++++++----- test/testuninitvar.cpp | 159 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 219 insertions(+), 23 deletions(-) diff --git a/cfg/std.cfg b/cfg/std.cfg index 87828861e..812fa381d 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -15,10 +15,20 @@ fopen - false - false - false - + false + false + false + false + false + + false + + + 0- + + + false + false 0-255 false 0-255 false 0-255 @@ -54,7 +64,7 @@ false false false - + 0,2-36 0,2-36 0,2-36 @@ -64,6 +74,10 @@ 0,2-36 0,2-36 + + false + false + false false false false diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 90ea10ccc..6562667bc 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -201,7 +201,7 @@ static const Signaltype listofsignals[] = { }; /** - * Simple helper function: + * Simple helper function: * \return size of array * */ template diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index a7a388921..f49612b9d 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -2396,54 +2396,77 @@ private: 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"; - // str.. - check("void f(char*p){ strchr (p,c);if(!p){}}"); + check("void f(FILE *p){ clearerr (p);if(!p){}}"); ASSERT_EQUALS(errp,errout.str()); - check("void f(char*p){ strdup (p);if(!p){}}"); + check("void f(FILE *p){ feof (p);if(!p){}}"); ASSERT_EQUALS(errp,errout.str()); - check("void f(char*p){ strlen (p);if(!p){}}"); + check("void f(FILE *p){ fgetc (p);if(!p){}}"); ASSERT_EQUALS(errp,errout.str()); - check("void f(char*p,char*q){ strcpy (p,q);if(!p||!q){}}"); + 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(char * p,char * q){ fopen (p,q);if(!p||!q){}}"); ASSERT_EQUALS(errpq,errout.str()); - check("void f(char*p,char*q){ strspn (p,q);if(!p||!q){}}"); + check("void f(FILE * p,fpos_t * q){ fgetpos (p,q);if(!p||!q){}}"); ASSERT_EQUALS(errpq,errout.str()); - check("void f(char*p,char*q){ strcspn (p,q);if(!p||!q){}}"); + check("void f(FILE * p,fpos_t * q){ fsetpos (p,q);if(!p||!q){}}"); ASSERT_EQUALS(errpq,errout.str()); - check("void f(char*p,char*q){ strcoll (p,q);if(!p||!q){}}"); + check("void f(char * p){ strchr (p,c);if(!p){}}"); + ASSERT_EQUALS(errp,errout.str()); + + check("void f(char * p){ strdup (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){ strcat (p,q);if(!p||!q){}}"); + 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){ strcmp (p,q);if(!p||!q){}}"); + 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){ strncpy (p,q,1);if(!p||!q){}}"); + 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){ strncat (p,q,1);if(!p||!q){}}"); + 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){ strncmp (p,q,1);if(!p||!q){}}"); + 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){ strstr (p,q);if(!p||!q){}}"); + 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){}}"); + 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){}}"); + 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){}}"); + check("void f(char * p,char * q){ strtol (p,q,0);if(!p){}}"); ASSERT_EQUALS(errp,errout.str()); } }; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 4d9533344..f99897364 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -67,6 +67,9 @@ private: TEST_CASE(uninitvar2_malloc); // malloc returns uninitialized data TEST_CASE(syntax_error); // Ticket #5073 + + // Test that std.cfg is configured correctly + TEST_CASE(stdcfg); } void checkUninitVar(const char code[], const char filename[] = "test.cpp") { @@ -3423,6 +3426,162 @@ private: "}", "test.cpp", /*verify=*/true, /*debugwarnings=*/true); ASSERT_EQUALS("[test.cpp:6]: (debug) assertion failed '} while ('\n", errout.str()); } + + // Test that std.cfg is configured correctly + void stdcfg() { + // clearerr + checkUninitVar("void f() {\n" + " FILE * pFile;\n" + " clearerr (pFile);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str()); + + checkUninitVar("void f(FILE * pFile) {\n" + " clearerr (pFile);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + // fclose + checkUninitVar("void f() {\n" + " FILE * pFile;\n" + " fclose (pFile);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str()); + + checkUninitVar("void f(FILE * pFile) {\n" + " fclose (pFile);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + // fopen + checkUninitVar("void f() {\n" + " char * filename;\n" + " fopen (filename, \"w\");\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: filename\n", errout.str()); + + checkUninitVar("void f() {\n" + " char * filename;\n" + " char * mode;\n" + " fopen (filename, mode);\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: filename\n" + "[test.cpp:4]: (error) Uninitialized variable: mode\n", errout.str()); + + checkUninitVar("void f(FILE * name, char *mode) {\n" + " fopen (name, mode);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + // feof + checkUninitVar("void f() {\n" + " FILE * pFile;\n" + " feof (pFile);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str()); + + checkUninitVar("void f(FILE * pFile) {\n" + " feof (pFile);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + // ferror + checkUninitVar("void f() {\n" + " FILE * pFile;\n" + " ferror (pFile);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str()); + + checkUninitVar("void f(FILE * pFile) {\n" + " ferror (pFile);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + // fflush + checkUninitVar("void f() {\n" + " FILE * pFile;\n" + " fflush (pFile);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str()); + + checkUninitVar("void f(FILE * pFile) {\n" + " fflush (pFile);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + // fgetc + checkUninitVar("void f() {\n" + " FILE * pFile;\n" + " fgetc (pFile);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: pFile\n", errout.str()); + + checkUninitVar("void f(FILE * pFile) {\n" + " fgetc (pFile);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + + // fgetpos + checkUninitVar("void f() {\n" + " FILE * f;\n" + " fpos_t * p;\n" + " fgetpos (f, p);\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: f\n" + "[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str()); + + checkUninitVar("void f(FILE * f) {\n" + " fpos_t p;" + " fgetpos (f, &p);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + checkUninitVar("void f(FILE * f, fpos_t *p) {\n" + " fgetpos (f, p);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + // fsetpos + checkUninitVar("void f() {\n" + " FILE * f;\n" + " fpos_t * p;\n" + " fsetpos (f, p);\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: f\n" + "[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str()); + + checkUninitVar("void f(FILE * f) {\n" + " fpos_t *p;" + " fsetpos (f, p);\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: p\n", errout.str()); + + checkUninitVar("void f(FILE * f, fpos_t *p) {\n" + " fsetpos (f, p);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + // fgets + checkUninitVar("void f(FILE *f) {\n" + " char *s;\n" + " int n;\n" + " fgets (s, n, f);\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: n\n" + "[test.cpp:4]: (error) Uninitialized variable: s\n", errout.str()); + + checkUninitVar("void f(char * s, int n) {\n" + " FILE *f;\n" + " fgets (s, n, f);\n" + "}"); + TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: f\n","", errout.str()); + + checkUninitVar("void f(char * s, int n, FILE *f) {\n" + " fgets (s, n, f);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestUninitVar)