Fixed #2775 (Uninitialized variable: Not detected when uninitialized struct pointer is dereferenced in sub function)

This commit is contained in:
Daniel Marjamäki 2011-07-04 21:04:32 +02:00
parent 5561d81f0d
commit 982b645ff4
2 changed files with 20 additions and 0 deletions

View File

@ -1047,6 +1047,14 @@ public:
continue;
}
if (Token::Match(tok2, "const| struct| %type% * %var% ,|)"))
{
while (tok2->isName())
tok2 = tok2->next();
tok2 = tok2->tokAt(2);
continue;
}
break;
}

View File

@ -1296,6 +1296,7 @@ private:
ASSERT_EQUALS("rename", analyseFunctions("int rename (const char oldname[], const char newname[]);"));
ASSERT_EQUALS("", analyseFunctions("void foo(int &x) { x = 0; }"));
ASSERT_EQUALS("", analyseFunctions("void foo(s x) { }"));
ASSERT_EQUALS("foo", analyseFunctions("void foo(Fred *fred) { fred->x = 0; }"));
// function calls..
checkUninitVar("void assignOne(int &x)\n"
@ -1448,6 +1449,17 @@ private:
" fread(buf, 1, 10, f);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #2775 - uninitialized struct pointer in subfunction
checkUninitVar("void a(struct Fred *fred) {\n"
" fred->x = 0;\n"
"}\n"
"\n"
"void b() {\n"
" struct Fred *p;\n"
" a(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: p\n", errout.str());
}
// valid and invalid use of 'int a(int x) { return x + x; }'