Fixed #4998 (False positive: Address of local auto-variable assigned to a function parameter)

This commit is contained in:
Daniel Marjamäki 2013-10-06 14:23:26 +02:00
parent 9f23b7a1b4
commit 1234ec95f0
2 changed files with 16 additions and 0 deletions

View File

@ -90,6 +90,9 @@ static bool checkRvalueExpression(const Token * const vartok)
const Token * const next = vartok->next(); const Token * const next = vartok->next();
if (Token::Match(vartok->previous(), "& %var% [") && var->isPointer())
return false;
// &a.b[0] // &a.b[0]
if (Token::Match(vartok, "%var% . %var% [") && !var->isPointer()) { if (Token::Match(vartok, "%var% . %var% [") && !var->isPointer()) {
const Variable *var2 = next->next()->variable(); const Variable *var2 = next->next()->variable();

View File

@ -325,6 +325,19 @@ private:
" *p = &a.data[0];\n" " *p = &a.data[0];\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
// #4998
check("void f(s8**out) {\n"
" s8 *p;\n" // <- p is pointer => no error
" *out = &p[1];\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(s8**out) {\n"
" s8 p[10];\n" // <- p is array => error
" *out = &p[1];\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());
} }
void testautovar12() { // Ticket #5024 - Crash on invalid input void testautovar12() { // Ticket #5024 - Crash on invalid input