Refactoring TestUninitVar. Created TestUninitVar::uninitvar_return

This commit is contained in:
Daniel Marjamäki 2012-06-23 16:27:57 +02:00
parent b6f4dbd067
commit 1e1b20d113
1 changed files with 106 additions and 106 deletions

View File

@ -42,6 +42,7 @@ private:
TEST_CASE(uninitvar_loops); // handling for/while TEST_CASE(uninitvar_loops); // handling for/while
TEST_CASE(uninitvar_switch); // handling switch TEST_CASE(uninitvar_switch); // handling switch
TEST_CASE(uninitvar_references); // references TEST_CASE(uninitvar_references); // references
TEST_CASE(uninitvar_return); // return
TEST_CASE(uninitvar_strncpy); // strncpy doesn't always 0-terminate TEST_CASE(uninitvar_strncpy); // strncpy doesn't always 0-terminate
TEST_CASE(uninitvar_memset); // not 0-terminated TEST_CASE(uninitvar_memset); // not 0-terminated
TEST_CASE(uninitvar_func); // analyse functions TEST_CASE(uninitvar_func); // analyse functions
@ -82,7 +83,6 @@ private:
" int a;\n" " int a;\n"
" int b = 1;\n" " int b = 1;\n"
" (b += a) = 1;\n" " (b += a) = 1;\n"
" return b*a;\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n","", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n","", errout.str());
@ -202,40 +202,6 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("static int foo()\n"
"{\n"
" int ret;\n"
" return ret;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ret\n", errout.str());
checkUninitVar("static int foo()\n"
"{\n"
" int ret;\n"
" return ret+5;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ret\n", errout.str());
checkUninitVar("static int foo() {\n"
" int ret;\n"
" return ret = 5;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
{
checkUninitVar("static int foo() {\n"
" int ret;\n"
" return cin >> ret;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("static int foo() {\n"
" int ret;\n"
" return cin >> ret;\n"
"}\n", "test.c");
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
}
checkUninitVar("void f()\n" checkUninitVar("void f()\n"
"{\n" "{\n"
" int a;\n" " int a;\n"
@ -283,7 +249,7 @@ private:
" int i;\n" " int i;\n"
" if (x)\n" " if (x)\n"
" i = 0;\n" " i = 0;\n"
" return i;\n" " i++;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: i\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: i\n", errout.str());
@ -317,15 +283,16 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Unknown types
{ {
checkUninitVar("A a()\n" checkUninitVar("void a()\n"
"{\n" "{\n"
" A ret;\n" " A ret;\n"
" return ret;\n" " return ret;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("A a()\n" checkUninitVar("void a()\n"
"{\n" "{\n"
" A ret;\n" " A ret;\n"
" return ret;\n" " return ret;\n"
@ -334,13 +301,6 @@ private:
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ret\n", errout.str()); ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ret\n", errout.str());
} }
checkUninitVar("int a()\n"
"{\n"
" int x;\n"
" return x;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
checkUninitVar("void a()\n" checkUninitVar("void a()\n"
"{\n" "{\n"
" int x[10];\n" " int x[10];\n"
@ -366,17 +326,19 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Handling >> and <<
{
checkUninitVar("int a() {\n" checkUninitVar("int a() {\n"
" int ret;\n" " int ret;\n"
" std::cin >> ret;\n" " std::cin >> ret;\n"
" return ret;\n" " ret++;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("int a() {\n" checkUninitVar("int a() {\n"
" int ret;\n" " int ret;\n"
" int a = value >> ret;\n" " int a = value >> ret;\n"
" return ret;\n" " ret++;\n"
"}\n", "}\n",
"test.c"); "test.c");
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str()); ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
@ -402,12 +364,12 @@ private:
"}\n", "}\n",
"test.c"); "test.c");
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str()); ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
}
checkUninitVar("int a()\n" checkUninitVar("void a() {\n" // asm
"{\n" " int x;\n"
" int ret;\n"
" asm();\n" " asm();\n"
" return ret;\n" " x++;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
@ -448,7 +410,7 @@ private:
" char key;\n" " char key;\n"
" struct A msg = { .buf = {&key} };\n" " struct A msg = { .buf = {&key} };\n"
" init(&msg);\n" " init(&msg);\n"
" return key;\n" " key++;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
@ -568,28 +530,6 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Ticket #2146 - False negative
checkUninitVar("int f(int x) {\n"
" int y;\n"
" return x ? 1 : y;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: y\n", errout.str());
{
// Ticket #3106 - False positive
checkUninitVar("int f() {\n"
" int i;\n"
" return x(&i) ? i : 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("int f() {\n"
" int i;\n"
" return x() ? i : 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", errout.str());
}
// Ticket #3480 - Don't crash garbage code // Ticket #3480 - Don't crash garbage code
checkUninitVar("int f()\n" checkUninitVar("int f()\n"
"{\n" "{\n"
@ -1487,6 +1427,66 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void uninitvar_return() {
checkUninitVar("static int foo()\n"
"{\n"
" int ret;\n"
" return ret;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ret\n", errout.str());
checkUninitVar("static int foo()\n"
"{\n"
" int ret;\n"
" return ret+5;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ret\n", errout.str());
checkUninitVar("static int foo() {\n"
" int ret;\n"
" return ret = 5;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
{
checkUninitVar("static int foo() {\n"
" int ret;\n"
" return cin >> ret;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("static int foo() {\n"
" int ret;\n"
" return cin >> ret;\n"
"}\n", "test.c");
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
}
// Ticket #2146 - False negative
checkUninitVar("int f(int x) {\n"
" int y;\n"
" return x ? 1 : y;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: y\n", errout.str());
// Ticket #3106 - False positive
{
checkUninitVar("int f() {\n"
" int i;\n"
" return x(&i) ? i : 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("int f() {\n"
" int i;\n"
" return x() ? i : 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", errout.str());
}
}
// strncpy doesn't always 0-terminate.. // strncpy doesn't always 0-terminate..
void uninitvar_strncpy() { void uninitvar_strncpy() {
checkUninitVar("void f()\n" checkUninitVar("void f()\n"