From 5cb701d3c1d7f848d231e14e8f41768e9f6a0f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 27 Jul 2011 10:34:12 +0200 Subject: [PATCH] Fixed #2946 (False positive: Uninitialized buffer variable (init in subfunction)) --- lib/checkuninitvar.cpp | 3 +++ test/testuninitvar.cpp | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index bf27dec8d..f398d5be6 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1073,6 +1073,8 @@ public: continue; } + /// @todo enable this code. if pointer is written in function then dead pointer is invalid but valid pointer is ok. + /* if (Token::Match(tok2, "const| struct| %type% * %var% ,|)")) { while (tok2->isName()) @@ -1080,6 +1082,7 @@ public: tok2 = tok2->tokAt(2); continue; } + */ break; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index e4f13bead..3ec91944d 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1326,7 +1326,9 @@ 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; }")); + // TODO: it's ok to pass a valid pointer to "foo". See #2775 and #2946 + TODO_ASSERT_EQUALS("foo", "", analyseFunctions("void foo(Fred *fred) { fred->x = 0; }")); + ASSERT_EQUALS("", analyseFunctions("void foo(int *x) { x[0] = 0; }")); // function calls.. checkUninitVar("void assignOne(int &x)\n" @@ -1489,7 +1491,19 @@ private: " struct Fred *p;\n" " a(p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: p\n", errout.str()); + // TODO: See #2946 + TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: p\n", "", errout.str()); + + // #2946 - FP array is initialized in subfunction + checkUninitVar("void a(char *buf) {\n" + " buf[0] = 0;\n" + "}\n" + "void b() {\n" + " char buf[10];\n" + " a(buf);\n" + " buf[1] = buf[0];\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // valid and invalid use of 'int a(int x) { return x + x; }'