From 982b645ff45ac2e45bffe0f09ad3e784614e438a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 4 Jul 2011 21:04:32 +0200 Subject: [PATCH] Fixed #2775 (Uninitialized variable: Not detected when uninitialized struct pointer is dereferenced in sub function) --- lib/checkuninitvar.cpp | 8 ++++++++ test/testuninitvar.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 0e9f22e86..577863b22 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -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; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 9d6cb849f..73e9db1cb 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -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; }'