CheckUninitVar: Fixed FP when dereferencing multidimensional arrays. Refactoring of testing. The FP was spotted when looking at #7092

This commit is contained in:
Daniel Marjamäki 2016-08-02 14:27:51 +02:00
parent 99b23012aa
commit 2187e8ba02
2 changed files with 32 additions and 19 deletions

View File

@ -918,9 +918,12 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
if (vartok->previous()->str() != "&" || !Token::Match(vartok->tokAt(-2), "[(,=?:]")) { if (vartok->previous()->str() != "&" || !Token::Match(vartok->tokAt(-2), "[(,=?:]")) {
if (alloc != NO_ALLOC && vartok->previous()->str() == "*") { if (alloc != NO_ALLOC && vartok->previous()->str() == "*") {
// TestUninitVar::isVariableUsageDeref()
const Token *parent = vartok->previous()->astParent(); const Token *parent = vartok->previous()->astParent();
if (parent && parent->str() == "=" && parent->astOperand1() == vartok->previous()) if (parent && parent->str() == "=" && parent->astOperand1() == vartok->previous())
return false; return false;
if (vartok->variable() && vartok->variable()->dimensions().size() >= 2)
return false;
return true; return true;
} }
return alloc == NO_ALLOC; return alloc == NO_ALLOC;

View File

@ -73,6 +73,8 @@ private:
TEST_CASE(syntax_error); // Ticket #5073 TEST_CASE(syntax_error); // Ticket #5073
TEST_CASE(trac_5970); TEST_CASE(trac_5970);
TEST_CASE(isVariableUsageDeref); // *p
// dead pointer // dead pointer
TEST_CASE(deadPointer); TEST_CASE(deadPointer);
} }
@ -537,13 +539,6 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar("void f()\n"
"{\n"
" char *s = malloc(100);\n"
" *s += 10;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: s\n", errout.str());
checkUninitVar("void f()\n" checkUninitVar("void f()\n"
"{\n" "{\n"
" int a[10];\n" " int a[10];\n"
@ -1236,18 +1231,6 @@ private:
"}"); "}");
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());
checkUninitVar("int f() {\n"
" char a[10];\n"
" char c = *a;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("int f() {\n"
" char a[SIZE+10];\n"
" char c = *a;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("int f()\n" checkUninitVar("int f()\n"
"{\n" "{\n"
" char a[10];\n" " char a[10];\n"
@ -3783,6 +3766,33 @@ private:
check.deadPointer(); check.deadPointer();
} }
void isVariableUsageDeref() {
// *p
checkUninitVar("void f() {\n"
" char a[10];\n"
" char c = *a;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void f() {\n"
" char a[SIZE+10];\n"
" char c = *a;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void f() {\n"
" char a[10];\n"
" *a += 10;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void f() {\n"
" int a[10][10];\n"
" dostuff(*a);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void deadPointer() { void deadPointer() {
checkDeadPointer("void f() {\n" checkDeadPointer("void f() {\n"
" int *p = p1;\n" " int *p = p1;\n"