Uninitialized variables: improved checking of c++ code

This commit is contained in:
Daniel Marjamäki 2013-02-15 18:13:47 +01:00
parent ccd95d1749
commit 382d7a10e8
2 changed files with 21 additions and 13 deletions

View File

@ -1059,7 +1059,8 @@ void CheckUninitVar::check()
void CheckUninitVar::checkScope(const Scope* scope) void CheckUninitVar::checkScope(const Scope* scope)
{ {
for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) { for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) {
if ((_tokenizer->isCPP() && i->type() && !i->isPointer()) || i->isStatic() || i->isExtern() || i->isConst() || i->isArray() || i->isReference()) if ((_tokenizer->isCPP() && i->type() && !i->isPointer() && i->type()->needInitialization != Scope::True) ||
i->isStatic() || i->isExtern() || i->isConst() || i->isArray() || i->isReference())
continue; continue;
if (i->nameToken()->strAt(1) == "(") if (i->nameToken()->strAt(1) == "(")
continue; continue;

View File

@ -2568,34 +2568,41 @@ private:
} }
void uninitvar2_structmembers() { // struct members void uninitvar2_structmembers() { // struct members
checkUninitVar2("struct AB { int a; int b; };\n"
"void f(void) {\n"
" struct AB ab;\n"
" int a = ab.a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar2("struct AB { int a; int b; };\n" checkUninitVar2("struct AB { int a; int b; };\n"
"void do_something(const struct AB ab);\n" "void do_something(const struct AB ab);\n"
"void f(void) {\n" "void f(void) {\n"
" struct AB ab;\n" " struct AB ab;\n"
" ab.a = 0;\n" " ab.a = 0;\n"
" do_something(ab);\n" " do_something(ab);\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("[test.c:6]: (error) Uninitialized struct member: ab.b\n", errout.str()); ASSERT_EQUALS("[test.c:6]: (error) Uninitialized struct member: ab.b\n", errout.str());
checkUninitVar2("struct AB { int a; int b; };\n" checkUninitVar2("struct AB { int a; int b; };\n"
"void f(void) {\n" "void f(void) {\n"
" struct AB ab;\n" " struct AB ab;\n"
" int a = ab.a;\n" " int a = ab.a;\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str()); ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar2("struct AB { int a; int b; };\n" checkUninitVar2("struct AB { int a; int b; };\n"
"void f(void) {\n" "void f(void) {\n"
" struct AB ab;\n" " struct AB ab;\n"
" buf[ab.a] = 0;\n" " buf[ab.a] = 0;\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str()); ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar2("struct AB { int a; int b; };\n" // pass struct member by address checkUninitVar2("struct AB { int a; int b; };\n" // pass struct member by address
"void f(void) {\n" "void f(void) {\n"
" struct AB ab;\n" " struct AB ab;\n"
" assign(&ab.a, 0);\n" " assign(&ab.a, 0);\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar2("struct AB { int a; int b; };\n" checkUninitVar2("struct AB { int a; int b; };\n"
@ -2605,7 +2612,7 @@ private:
" ab.a = 0;\n" " ab.a = 0;\n"
" ab.b = 0;\n" " ab.b = 0;\n"
" do_something(ab);\n" " do_something(ab);\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar2("struct AB { int a; int b; };\n" checkUninitVar2("struct AB { int a; int b; };\n"
@ -2614,7 +2621,7 @@ private:
" struct AB ab;\n" " struct AB ab;\n"
" ab = getAB();\n" " ab = getAB();\n"
" do_something(ab);\n" " do_something(ab);\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar2("struct AB { int a; struct { int b; int c; } s; };\n" checkUninitVar2("struct AB { int a; struct { int b; int c; } s; };\n"
@ -2625,7 +2632,7 @@ private:
" ab.s.b = 2;\n" " ab.s.b = 2;\n"
" ab.s.c = 3;\n" " ab.s.c = 3;\n"
" do_something(ab);\n" " do_something(ab);\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar2("struct conf {\n" checkUninitVar2("struct conf {\n"
@ -2638,7 +2645,7 @@ private:
" struct conf c;\n" " struct conf c;\n"
" initdata(&c);\n" " initdata(&c);\n"
" do_something(c);\n" " do_something(c);\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// return // return
@ -2647,7 +2654,7 @@ private:
" struct AB ab;\n" " struct AB ab;\n"
" ab.a = 0;\n" " ab.a = 0;\n"
" return ab.b;\n" " return ab.b;\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.b\n", errout.str()); ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.b\n", errout.str());
checkUninitVar2("struct AB { int a; int b; };\n" checkUninitVar2("struct AB { int a; int b; };\n"
@ -2655,7 +2662,7 @@ private:
" struct AB ab;\n" " struct AB ab;\n"
" ab.a = 0;\n" " ab.a = 0;\n"
" return ab.a;\n" " return ab.a;\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// checkIfForWhileHead // checkIfForWhileHead
@ -2668,7 +2675,7 @@ private:
" struct FRED fred;\n" " struct FRED fred;\n"
" fred.a = do_something();\n" " fred.a = do_something();\n"
" if (fred.a == 0) { }\n" " if (fred.a == 0) { }\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar2("struct FRED {\n" checkUninitVar2("struct FRED {\n"
@ -2680,7 +2687,7 @@ private:
" struct FRED fred;\n" " struct FRED fred;\n"
" fred.a = do_something();\n" " fred.a = do_something();\n"
" if (fred.b == 0) { }\n" " if (fred.b == 0) { }\n"
"}\n", "test.c", true); "}\n", "test.c");
ASSERT_EQUALS("[test.c:9]: (error) Uninitialized struct member: fred.b\n", errout.str()); ASSERT_EQUALS("[test.c:9]: (error) Uninitialized struct member: fred.b\n", errout.str());
checkUninitVar2("struct S { int n; int m; };\n" checkUninitVar2("struct S { int n; int m; };\n"