Dead pointer: Fixed FP for subfunction pointer argument

This commit is contained in:
Daniel Marjamäki 2014-08-14 06:47:19 +02:00
parent e83f08a825
commit b8e356462a
2 changed files with 25 additions and 6 deletions

View File

@ -1318,8 +1318,17 @@ const Token *Token::getValueTokenMaxStrLength() const
return ret; return ret;
} }
static const Scope *getfunctionscope(const Scope *s)
{
while (s && s->type != Scope::eFunction)
s = s->nestedIn;
return s;
}
const Token *Token::getValueTokenDeadPointer() const const Token *Token::getValueTokenDeadPointer() const
{ {
const Scope * const functionscope = getfunctionscope(this->scope());
std::list<ValueFlow::Value>::const_iterator it; std::list<ValueFlow::Value>::const_iterator it;
for (it = values.begin(); it != values.end(); ++it) { for (it = values.begin(); it != values.end(); ++it) {
// Is this a pointer alias? // Is this a pointer alias?
@ -1332,6 +1341,10 @@ const Token *Token::getValueTokenDeadPointer() const
const Variable * const var = vartok->variable(); const Variable * const var = vartok->variable();
if (var->isStatic()) if (var->isStatic())
continue; continue;
// variable must be in same function (not in subfunction)
if (functionscope != getfunctionscope(var->scope()))
continue;
// Is variable defined in this scope or upper scope?
const Scope *s = this->scope(); const Scope *s = this->scope();
while ((s != nullptr) && (s != var->scope())) while ((s != nullptr) && (s != var->scope()))
s = s->nestedIn; s = s->nestedIn;

View File

@ -3739,14 +3739,20 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:7]: (error) Dead pointer usage. Pointer 'p' is dead if it has been assigned '&x' at line 5.\n", errout.str()); ASSERT_EQUALS("[test.cpp:7]: (error) Dead pointer usage. Pointer 'p' is dead if it has been assigned '&x' at line 5.\n", errout.str());
checkDeadPointer("void a(const int *p) {\n" // FP: don't warn in subfunction
" *p = 0;\n" checkDeadPointer("void f(struct KEY *key) {\n"
" key->x = 0;\n"
"}\n" "}\n"
"\n" "\n"
"void b() {\n" "int main() {\n"
" int x;\n" " struct KEY *tmp = 0;\n"
" int *p = &x;" " struct KEY k;\n"
" a(p);\n" "\n"
" if (condition) {\n"
" tmp = &k;\n"
" } else {\n"
" }\n"
" f(tmp);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }