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;
}
static const Scope *getfunctionscope(const Scope *s)
{
while (s && s->type != Scope::eFunction)
s = s->nestedIn;
return s;
}
const Token *Token::getValueTokenDeadPointer() const
{
const Scope * const functionscope = getfunctionscope(this->scope());
std::list<ValueFlow::Value>::const_iterator it;
for (it = values.begin(); it != values.end(); ++it) {
// Is this a pointer alias?
@ -1332,6 +1341,10 @@ const Token *Token::getValueTokenDeadPointer() const
const Variable * const var = vartok->variable();
if (var->isStatic())
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();
while ((s != nullptr) && (s != var->scope()))
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());
checkDeadPointer("void a(const int *p) {\n"
" *p = 0;\n"
// FP: don't warn in subfunction
checkDeadPointer("void f(struct KEY *key) {\n"
" key->x = 0;\n"
"}\n"
"\n"
"void b() {\n"
" int x;\n"
" int *p = &x;"
" a(p);\n"
"int main() {\n"
" struct KEY *tmp = 0;\n"
" struct KEY k;\n"
"\n"
" if (condition) {\n"
" tmp = &k;\n"
" } else {\n"
" }\n"
" f(tmp);\n"
"}");
ASSERT_EQUALS("", errout.str());
}