From 51ad9ab6ac9411e2de0b332dc47516bb73d3b399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 26 Oct 2013 18:39:40 +0200 Subject: [PATCH] Fixed #4896 (FP Uninitialized variable on template function) --- lib/checkuninitvar.cpp | 14 +++----------- test/testuninitvar.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index f31da168e..d6b348d43 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -373,6 +373,8 @@ private: break; if (Token::Match(tok2, "%var% (")) break; + if (Token::Match(tok2, "%var% <") && Token::Match(tok2->linkAt(1), "> (")) + break; if (tok2->varId() && !Token::Match(tok2->previous(), "&|::") && !Token::simpleMatch(tok2->tokAt(-2), "& (") && @@ -477,17 +479,7 @@ private: if (tok.varId()) { // array variable passed as function parameter.. if (Token::Match(tok.previous(), "[(,] %var% [+-,)]")) { - // skip ')'.. - const Token *tok2 = tok.next(); - while (tok2 && tok2->str() == ")") - tok2 = tok2->next(); - - // variable is assigned like: "( %var% ) .. =" - if (Token::Match(tok.previous(), "( %var% )") && tok2 && tok2->str() == "=") - ExecutionPath::bailOutVar(checks, tok.varId()); - else if (tok.strAt(-2) != ">" || !tok.linkAt(-2)) - use(checks, &tok); - //use_array(checks, &tok); + ExecutionPath::bailOutVar(checks, tok.varId()); return &tok; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 17cb06199..af1189465 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1740,6 +1740,13 @@ private: " int i;\n" " x(i+2);\n" "}"); + ASSERT_EQUALS("", errout.str()); + + checkUninitVar2("void f()\n" + "{\n" + " int i;\n" + " x(i+2);\n" + "}"); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n", errout.str()); checkUninitVar("void f()\n" @@ -1777,6 +1784,14 @@ private: " int x;\n" " foo(x);\n" "}"); + ASSERT_EQUALS("", errout.str()); + + checkUninitVar2("int foo(int x) { return x; }\n" + "void f2()\n" + "{\n" + " int x;\n" + " foo(x);\n" + "}"); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str()); checkUninitVar("void foo(const char *s)\n" @@ -1823,6 +1838,20 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + checkUninitVar("void test() {\n" + " double d;\n" + " double x = dostuff(d);\n" + " return x;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + checkUninitVar("template double dostuff(int x, T &y);\n" + "void test() {\n" + " double d;\n" + " a = dostuff(0, d);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // using uninitialized function pointer.. checkUninitVar("void foo()\n" "{\n" @@ -1913,6 +1942,13 @@ private: " int x;\n" " a(x);\n" "}").c_str()); + ASSERT_EQUALS("", errout.str()); + + checkUninitVar2((funca + + "void b() {\n" + " int x;\n" + " a(x);\n" + "}").c_str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str()); checkUninitVar((funca + @@ -1942,6 +1978,12 @@ private: " int *p;\n" " a(p);\n" "}").c_str()); + ASSERT_EQUALS("", errout.str()); + checkUninitVar2((funca + + "void b() {\n" + " int *p;\n" + " a(p);\n" + "}").c_str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str()); }