From b8e356462a647ea492f8870cd97d400f9abca451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 14 Aug 2014 06:47:19 +0200 Subject: [PATCH] Dead pointer: Fixed FP for subfunction pointer argument --- lib/token.cpp | 13 +++++++++++++ test/testuninitvar.cpp | 18 ++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index b69e5545b..06ba08388 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -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::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; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 2f9dbbaad..0d1b94a27 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -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()); }