From cfd6b0c38403f1ddbbb2496b52dac26f2802700b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 16 Nov 2013 12:02:21 +0100 Subject: [PATCH] UninitVar: fixed false negatives for function calls --- lib/checkuninitvar.cpp | 4 ++++ test/testuninitvar.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 977eaa550..c5eef3e47 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1628,6 +1628,8 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp const Token *argStart = arg->typeStartToken(); while (argStart->previous() && argStart->previous()->isName()) argStart = argStart->previous(); + if (!address && Token::Match(argStart, "const| struct| %type% [,)]")) + return true; if (!address && Token::Match(argStart, "const| struct| %type% %var% [,)]")) return true; if (Token::Match(argStart, "const %type% & %var% [,)]")) @@ -1636,6 +1638,8 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp return true; if ((pointer || address) && Token::Match(argStart, "const %type% * %var% [,)]")) return true; + if ((pointer || address) && Token::Match(argStart, "const %type% %var% [") && Token::Match(argStart->linkAt(3), "] [,)]")) + return true; } } else if (Token::Match(start->previous(), "if|while|for")) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index b9c9f5341..2f1815559 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2546,6 +2546,13 @@ private: // Handling of function calls void uninitvar2_func() { // non-pointer variable + checkUninitVar2("void a(char);\n" // value => error + "void b() {\n" + " char c;\n" + " a(c);\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str()); + checkUninitVar2("void a(char c);\n" // value => error "void b() {\n" " char c;\n" @@ -2611,6 +2618,13 @@ private: "}"); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str()); + checkUninitVar2("void a(const char c[]);\n" // const address => error + "void b() {\n" + " char *c;\n" + " a(c);\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str()); + checkUninitVar2("void a(char **c);\n" // address of pointer => no error "void b() {\n" " char *c;\n"