Merge pull request #485 from orbitcowboy/master

testuninitvar: changed test cases to do not rely on ExecutionPath.
This commit is contained in:
Martin Ettl 2014-12-16 08:09:01 +01:00
commit 119aa5092a
1 changed files with 401 additions and 396 deletions

View File

@ -624,78 +624,78 @@ private:
void uninitvar3() { // #3844 void uninitvar3() { // #3844
// avoid false positive // avoid false positive
checkUninitVar("namespace std _GLIBCXX_VISIBILITY(default)\n" checkUninitVar2("namespace std _GLIBCXX_VISIBILITY(default)\n"
"{\n" "{\n"
"_GLIBCXX_BEGIN_NAMESPACE_CONTAINER\n" "_GLIBCXX_BEGIN_NAMESPACE_CONTAINER\n"
" typedef unsigned long _Bit_type;\n" " typedef unsigned long _Bit_type;\n"
" struct _Bit_reference\n" " struct _Bit_reference\n"
" {\n" " {\n"
" _Bit_type * _M_p;\n" " _Bit_type * _M_p;\n"
" _Bit_type _M_mask;\n" " _Bit_type _M_mask;\n"
" _Bit_reference(_Bit_type * __x, _Bit_type __y)\n" " _Bit_reference(_Bit_type * __x, _Bit_type __y)\n"
" : _M_p(__x), _M_mask(__y) { }\n" " : _M_p(__x), _M_mask(__y) { }\n"
" };\n" " };\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void uninitvar_bitop() { void uninitvar_bitop() {
checkUninitVar("void foo() {\n" checkUninitVar2("void foo() {\n"
" int b;\n" " int b;\n"
" c = a | b;\n" " c = a | b;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: b\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: b\n", errout.str());
checkUninitVar("void foo() {\n" checkUninitVar2("void foo() {\n"
" int b;\n" " int b;\n"
" c = b | a;\n" " c = b | a;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: b\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: b\n", errout.str());
} }
// if.. // if..
void uninitvar_if() { void uninitvar_if() {
checkUninitVar("static void foo()\n" checkUninitVar2("static void foo()\n"
"{\n" "{\n"
" Foo *p;\n" " Foo *p;\n"
" if (x)\n" " if (x)\n"
" p = new Foo;\n" " p = new Foo;\n"
" p->abcd();\n" " p->abcd();\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: p\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: p\n", errout.str());
checkUninitVar("static void foo(int x)\n" checkUninitVar2("static void foo(int x)\n"
"{\n" "{\n"
" int a;\n" " int a;\n"
" if (x==1);\n" " if (x==1);\n"
" if (x==2);\n" " if (x==2);\n"
" x = a;\n" " x = a;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("int foo()\n" checkUninitVar2("int foo()\n"
"{\n" "{\n"
" int i;\n" " int i;\n"
" if (x)\n" " if (x)\n"
" i = 22;\n" " i = 22;\n"
" else\n" " else\n"
" i = 33;\n" " i = 33;\n"
" return i;\n" " return i;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo()\n" checkUninitVar2("int foo()\n"
"{\n" "{\n"
" int i;\n" " int i;\n"
" if (x)\n" " if (x)\n"
" i = 22;\n" " i = 22;\n"
" else\n" " else\n"
" {\n" " {\n"
" char *y = {0};\n" " char *y = {0};\n"
" i = 33;\n" " i = 33;\n"
" }\n" " }\n"
" return i;\n" " return i;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo()\n" checkUninitVar("int foo()\n"
@ -714,128 +714,128 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("static void foo(int x)\n" checkUninitVar2("static void foo(int x)\n"
"{\n" "{\n"
" Foo *p;\n" " Foo *p;\n"
" if (x)\n" " if (x)\n"
" p = new Foo;\n" " p = new Foo;\n"
" if (x)\n" " if (x)\n"
" p->abcd();\n" " p->abcd();\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo(int a)\n" checkUninitVar2("void foo(int a)\n"
"{\n" "{\n"
" int n;\n" " int n;\n"
" int condition;\n" " int condition;\n"
" if(a == 1) {\n" " if(a == 1) {\n"
" n=0;\n" " n=0;\n"
" condition=0;\n" " condition=0;\n"
" }\n" " }\n"
" else {\n" " else {\n"
" n=1;\n" " n=1;\n"
" }\n" " }\n"
"\n" "\n"
" if( n == 0) {\n" " if( n == 0) {\n"
" a=condition;\n" " a=condition;\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar2("void f()\n"
"{\n" "{\n"
" C *c;\n" " C *c;\n"
" if (fun(&c));\n" " if (fun(&c));\n"
" c->Release();\n" " c->Release();\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n" checkUninitVar2("void f() {\n"
" char a[10];\n" " char a[10];\n"
" if (a[0] = x){}\n" " if (a[0] = x){}\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo(int x)\n" checkUninitVar2("int foo(int x)\n"
"{\n" "{\n"
" int i;\n" " int i;\n"
" if (one())\n" " if (one())\n"
" i = 1;\n" " i = 1;\n"
" else\n" " else\n"
" return 3;\n" " return 3;\n"
" return i;\n" " return i;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Ticket #2207 - False positive // Ticket #2207 - False positive
checkUninitVar("void foo(int x) {\n" checkUninitVar2("void foo(int x) {\n"
" int a;\n" " int a;\n"
" if (x)\n" " if (x)\n"
" a = 1;\n" " a = 1;\n"
" if (!x)\n" " if (!x)\n"
" return;\n" " return;\n"
" b = (c - a);\n" " b = (c - a);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo()\n" checkUninitVar2("int foo()\n"
"{\n" "{\n"
" int ret;\n" " int ret;\n"
" if (one())\n" " if (one())\n"
" ret = 1;\n" " ret = 1;\n"
" else\n" " else\n"
" throw 3;\n" " throw 3;\n"
" return ret;\n" " return ret;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int f(int a)\n" checkUninitVar2("int f(int a)\n"
"{\n" "{\n"
" int ret;\n" " int ret;\n"
" if (a == 1)\n" " if (a == 1)\n"
" ret = 1;\n" " ret = 1;\n"
" else\n" " else\n"
" XYZ ret = 2;\n" // XYZ may be an unexpanded macro so bailout the checking of "ret". " XYZ ret = 2;\n" // XYZ may be an unexpanded macro so bailout the checking of "ret".
" return ret;\n" " return ret;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: ret\n", errout.str());
checkUninitVar2("int f(int a, int b)\n"
"{\n"
" int x;\n"
" if (a)\n"
" x = a;\n"
" else {\n"
" do { } while (f2());\n"
" x = b;\n"
" }\n"
" return x;\n"
"}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int f(int a, int b)\n" checkUninitVar2("void foo(long verbose,bool bFlag)\n"
"{\n" "{\n"
" int x;\n" " double t;\n"
" if (a)\n" " if (bFlag)\n"
" x = a;\n" " {\n"
" else {\n" " if (verbose)\n"
" do { } while (f2());\n" " t = 1;\n"
" x = b;\n" " if (verbose)\n"
" }\n" " std::cout << (12-t);\n"
" return x;\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo(long verbose,bool bFlag)\n" checkUninitVar2("int test(int cond1, int cond2) {\n"
"{\n" " int foo;\n"
" double t;\n" " if (cond1 || cond2) {\n"
" if (bFlag)\n" " if (cond2)\n"
" {\n" " foo = 0;\n"
" if (verbose)\n" " }\n"
" t = 1;\n" " if (cond2) {\n"
" if (verbose)\n" " int t = foo*foo;\n"
" std::cout << (12-t);\n" " }\n"
" }\n" "}");
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("int test(int cond1, int cond2) {\n"
" int foo;\n"
" if (cond1 || cond2) {\n"
" if (cond2)\n"
" foo = 0;\n"
" }\n"
" if (cond2) {\n"
" int t = foo*foo;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// ? : // ? :
@ -846,47 +846,47 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
checkUninitVar("void foo()\n" checkUninitVar2("void foo()\n"
"{\n" "{\n"
" const char *msgid1, *msgid2;\n" " const char *msgid1, *msgid2;\n"
" int ret = bar(&msgid1);\n" " int ret = bar(&msgid1);\n"
" if (ret > 0) {\n" " if (ret > 0) {\n"
" ret = bar(&msgid2);\n" " ret = bar(&msgid2);\n"
" }\n" " }\n"
" ret = ret <= 0 ? -1 :\n" " ret = ret <= 0 ? -1 :\n"
" strcmp(msgid1, msgid2) == 0;\n" " strcmp(msgid1, msgid2) == 0;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo(int a, int b)\n" checkUninitVar2("void foo(int a, int b)\n"
"{\n" "{\n"
" int x; x = (a<b) ? 1 : 0;\n" " int x; x = (a<b) ? 1 : 0;\n"
" int y = y;\n" " int y = y;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: y\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: y\n", errout.str());
// ; { .. } // ; { .. }
checkUninitVar("int foo()\n" checkUninitVar2("int foo()\n"
"{\n" "{\n"
" int retval;\n" " int retval;\n"
" if (condition) {\n" " if (condition) {\n"
" { }\n" " { }\n"
" retval = 1; }\n" " retval = 1; }\n"
" else\n" " else\n"
" retval = 2;\n" " retval = 2;\n"
" return retval;\n" " return retval;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo()\n" checkUninitVar2("void foo()\n"
"{\n" "{\n"
" {\n" " {\n"
" for (int i = 0; i < 10; ++i)\n" " for (int i = 0; i < 10; ++i)\n"
" { }\n" " { }\n"
" }\n" " }\n"
"\n" "\n"
" { }\n" " { }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Ticket #3098 - False negative uninitialized variable // Ticket #3098 - False negative uninitialized variable
@ -900,25 +900,25 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c1\n" ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c1\n"
"[test.cpp:4]: (error) Uninitialized variable: c2\n", errout.str()); "[test.cpp:4]: (error) Uninitialized variable: c2\n", errout.str());
checkUninitVar("void f(char *c1, char *c2)\n" checkUninitVar2("void f(char *c1, char *c2)\n"
"{\n" "{\n"
" if(strcoll(c1,c2))\n" " if(strcoll(c1,c2))\n"
" {\n" " {\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar2("void f()\n"
"{\n" "{\n"
" char *c1;\n" " char *c1;\n"
" c1=strcpy(c1,\"test\");\n" " c1=strcpy(c1,\"test\");\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c1\n","", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c1\n","", errout.str());
checkUninitVar("void f(char *c1)\n" checkUninitVar2("void f(char *c1)\n"
"{\n" "{\n"
" c1=strcpy(c1,\"test\");\n" " c1=strcpy(c1,\"test\");\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -927,49 +927,49 @@ private:
// handling for/while loops.. // handling for/while loops..
void uninitvar_loops() { void uninitvar_loops() {
// for.. // for..
checkUninitVar("void f()\n" checkUninitVar2("void f()\n"
"{\n" "{\n"
" for (int i = 0; i < 4; ++i) {\n" " for (int i = 0; i < 4; ++i) {\n"
" int a;\n" " int a;\n"
" b(4*a);\n" " b(4*a);\n"
" }" " }"
"}"); "}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void f() {\n" checkUninitVar2("void f() {\n"
" int k;\n" " int k;\n"
" for (int i = 0; i < 4; ++i) {\n" " for (int i = 0; i < 4; ++i) {\n"
" k = k + 2;\n" " k = k + 2;\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: k\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: k\n", errout.str());
checkUninitVar("void f() {\n" checkUninitVar2("void f() {\n"
" gchar sel[10];\n" " gchar sel[10];\n"
" for (int i = 0; i < 4; ++i) {\n" " for (int i = 0; i < 4; ++i) {\n"
" int sz = sizeof(sel);\n" " int sz = sizeof(sel);\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("enum ABCD { A, B, C, D };\n" checkUninitVar2("enum ABCD { A, B, C, D };\n"
"\n" "\n"
"static void f(char *str ) {\n" "static void f(char *str ) {\n"
" enum ABCD i;\n" " enum ABCD i;\n"
" for (i = 0; i < D; i++) {\n" " for (i = 0; i < D; i++) {\n"
" str[i] = 0;\n" " str[i] = 0;\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void x() {\n" checkUninitVar2("void x() {\n"
" do {\n" " do {\n"
" Token * tok;\n" " Token * tok;\n"
" for (tok = a; tok; tok = tok->next())\n" " for (tok = a; tok; tok = tok->next())\n"
" {\n" " {\n"
" }\n" " }\n"
" } while (tok2);\n" " } while (tok2);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// while.. // while..
@ -982,32 +982,32 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: i\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: i\n", errout.str());
checkUninitVar("void f(int i)\n" checkUninitVar2("void f(int i)\n"
"{\n" "{\n"
" int a;\n" " int a;\n"
" while (i < 10)\n" " while (i < 10)\n"
" i++;\n" " i++;\n"
" a++;" " a++;"
"}"); "}");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: a\n", errout.str());
// Ticket #2226: C++0x loop // Ticket #2226: C++0x loop
checkUninitVar("void f() {\n" checkUninitVar2("void f() {\n"
" container c;\n" " container c;\n"
" for (iterator it : c) {\n" " for (iterator it : c) {\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Ticket #2345: False positive in sub-condition in if inside a loop // Ticket #2345: False positive in sub-condition in if inside a loop
checkUninitVar("void f(int x) {\n" checkUninitVar2("void f(int x) {\n"
" const PoolItem* pItem;\n" " const PoolItem* pItem;\n"
" while (x > 0) {\n" " while (x > 0) {\n"
" if (GetItem(&pItem) && (*pItem != rPool))\n" " if (GetItem(&pItem) && (*pItem != rPool))\n"
" { }\n" " { }\n"
" x--;\n" " x--;\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f(int x) {\n" checkUninitVar("void f(int x) {\n"
" const PoolItem* pItem;\n" " const PoolItem* pItem;\n"
@ -1036,23 +1036,23 @@ private:
ASSERT_EQUALS("[test.cpp:11]: (error) Uninitialized variable: x\n", errout.str()); ASSERT_EQUALS("[test.cpp:11]: (error) Uninitialized variable: x\n", errout.str());
// Ticket #2796 // Ticket #2796
checkUninitVar("void foo() {\n" checkUninitVar2("void foo() {\n"
" while (true) {\n" " while (true) {\n"
" int x;\n" " int x;\n"
" if (y) x = 0;\n" " if (y) x = 0;\n"
" else break;\n" " else break;\n"
" return x;\n" // <- x is initialized " return x;\n" // <- x is initialized
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Assignment in for. Ticket #3287 // Assignment in for. Ticket #3287
checkUninitVar("int foo(char* in, bool b) {\n" checkUninitVar2("int foo(char* in, bool b) {\n"
" char* c;\n" " char* c;\n"
" if (b) for (c = in; *c == 0; ++c) {}\n" " if (b) for (c = in; *c == 0; ++c) {}\n"
" else c = in + strlen(in) - 1;\n" " else c = in + strlen(in) - 1;\n"
" *c = 0;\n" " *c = 0;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1069,41 +1069,40 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: c\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar("char * f()\n" checkUninitVar2("char * f()\n"
"{\n" "{\n"
" static char ret[200];\n" " static char ret[200];\n"
" memset(ret, 0, sizeof(ret));\n" " memset(ret, 0, 200);\n"
" switch (x)\n" " switch (x)\n"
" {\n" " {\n"
" case 1: return ret;\n" " case 1: return ret;\n"
" case 2: return ret;\n" " case 2: return ret;\n"
" }\n" " }\n"
" return 0;\n" " return 0;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo(const int iVar, unsigned int slot, unsigned int pin)\n" checkUninitVar2("int foo(const int iVar, unsigned int slot, unsigned int pin)\n"
"{\n" "{\n"
" int i;\n" " int i;\n"
"\n" " if (iVar == 0)\n"
" if (iVar == 0)\n" " {\n"
" {\n" " switch (slot)\n"
" switch (slot)\n" " {\n"
" {\n" " case 4: return 5;\n"
" case 4: return 5;\n" " default: return -1;\n"
" default: return -1;\n" " }\n"
" }\n" " }\n"
" }\n" " else\n"
" else\n" " {\n"
" {\n" " switch (pin)\n"
" switch (pin)\n" " {\n"
" {\n" " case 0: i = 2; break;\n"
" case 0: i = 2; break;\n" " default: i = -1; break;\n"
" default: i = -1; break;\n" " }\n"
" }\n" " }\n"
" }\n" " return i;\n"
" return i;\n" "}");
"}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// No segmentation fault // No segmentation fault
@ -1122,15 +1121,15 @@ private:
"}"); "}");
// #1855 - switch(foo(&x)) // #1855 - switch(foo(&x))
checkUninitVar("int a()\n" checkUninitVar2("int a()\n"
"{\n" "{\n"
" int x;\n" " int x;\n"
" switch (foo(&x))\n" " switch (foo(&x))\n"
" {\n" " {\n"
" case 1:\n" " case 1:\n"
" return x;\n" " return x;\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #3231 - ({ switch .. }) // #3231 - ({ switch .. })
@ -1179,38 +1178,38 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar2("void f()\n"
"{\n" "{\n"
" char a, b[10];\n" " char a, b[10];\n"
" a = b[0] = 0;\n" " a = b[0] = 0;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar2("void f()\n"
"{\n" "{\n"
" char a[10], b[10];\n" " char a[10], b[10];\n"
" a[0] = b[0] = 0;\n" " a[0] = b[0] = 0;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar2("void f()\n"
"{\n" "{\n"
" char a[10], *p;\n" " char a[10], *p;\n"
" *(p = a) = 0;\n" " *(p = a) = 0;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n" checkUninitVar2("void f() {\n"
" char a[10], *p;\n" " char a[10], *p;\n"
" p = &(a[10]);\n" " p = &(a[10]);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar2("void f()\n"
"{\n" "{\n"
" char c[50] = \"\";\n" " char c[50] = \"\";\n"
" strcat(c, \"test\");\n" " strcat(c, \"test\");\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar("void f()\n"
@ -1243,26 +1242,26 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo()\n" checkUninitVar2("void foo()\n"
"{\n" "{\n"
" int a[2];\n" " int a[2];\n"
" init(a - 1);\n" " init(a - 1);\n"
" int b = a[0];\n" " int b = a[0];\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo()\n" checkUninitVar2("void foo()\n"
"{\n" "{\n"
" Fred a[2];\n" " Fred a[2];\n"
" Fred b = a[0];\n" " Fred b = a[0];\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Ticket #2320 // Ticket #2320
checkUninitVar("void foo() {\n" checkUninitVar2("void foo() {\n"
" char a[2];\n" " char a[2];\n"
" char *b = (a+2) & 7;\n" " char *b = (a+2) & 7;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n" // Ticket #3050 checkUninitVar("void f() {\n" // Ticket #3050
@ -1271,34 +1270,34 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void f() {\n" // Ticket #5108 (fp) checkUninitVar2("void f() {\n" // Ticket #5108 (fp)
" const char *a;\n" " const char *a;\n"
" printf(\"%s\", a=\"abc\");\n" " printf(\"%s\", a=\"abc\");\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n" // Ticket #3497 checkUninitVar2("void f() {\n" // Ticket #3497
" char header[1];\n" " char header[1];\n"
" *((unsigned char*)(header)) = 0xff;\n" " *((unsigned char*)(header)) = 0xff;\n"
" return header[0];\n" " return header[0];\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n" // Ticket #3497 checkUninitVar2("void f() {\n" // Ticket #3497
" char header[1];\n" " char header[1];\n"
" *((unsigned char*)((unsigned char *)(header))) = 0xff;\n" " *((unsigned char*)((unsigned char *)(header))) = 0xff;\n"
" return header[0];\n" " return header[0];\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n" checkUninitVar2("void f() {\n"
" ABC abc;\n" " ABC abc;\n"
" int a[1];\n" " int a[1];\n"
"\n" "\n"
" abc.a = a;\n" " abc.a = a;\n"
" init(&abc);\n" " init(&abc);\n"
" return a[0];\n" " return a[0];\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// ticket #3344 // ticket #3344
@ -1311,16 +1310,16 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout.str());
// #3845 // #3845
checkUninitVar("int foo() {\n" checkUninitVar2("int foo() {\n"
" int a[1] = {5};\n" " int a[1] = {5};\n"
" return a[0];\n" " return a[0];\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo() {\n" checkUninitVar2("int foo() {\n"
" int a[2][2] = {{3,4}, {5,6}};\n" " int a[2][2] = {{3,4}, {5,6}};\n"
" return a[0][1];\n" " return a[0][1];\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo() {\n" checkUninitVar("int foo() {\n"
@ -1329,17 +1328,17 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("int foo() {\n" checkUninitVar2("int foo() {\n"
" int a[2][2];\n" " int a[2][2];\n"
" return a[0][1];\n" " return a[0][1];\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", "", errout.str());
// # 4740 // # 4740
checkUninitVar("void f() {\n" checkUninitVar2("void f() {\n"
" int *a[2][19];\n" " int *a[2][19];\n"
" int **b = a[0];\n" " int **b = a[0];\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -2081,6 +2080,9 @@ private:
// Tokenize.. // Tokenize..
Settings settings1(settings); Settings settings1(settings);
settings1.inconclusive = true;
settings1.standards.posix = true;
settings1.experimental = true;
settings1.debugwarnings = debugwarnings; settings1.debugwarnings = debugwarnings;
Tokenizer tokenizer(&settings1, this); Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code); std::istringstream istr(code);
@ -3142,7 +3144,10 @@ private:
" foo(123, &abc);\n" " foo(123, &abc);\n"
" return abc.b;\n" " return abc.b;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized struct member: abc.b\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: abc.a\n"
"[test.cpp:5]: (error) Uninitialized struct member: abc.b\n"
"[test.cpp:6]: (error) Uninitialized struct member: abc.b\n"
"[test.cpp:5]: (error) Uninitialized struct member: abc.c\n", errout.str());
// return // return
checkUninitVar2("struct AB { int a; int b; };\n" checkUninitVar2("struct AB { int a; int b; };\n"