From 2050cd71db8a56bce20f407276d6abc65b607d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 21 Dec 2012 19:32:56 +0100 Subject: [PATCH] Uninitialized variables: Better testing of function handling --- lib/checkuninitvar.cpp | 2 +- test/testuninitvar.cpp | 78 +++++++++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 6e60963e6..53e07bc26 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1315,7 +1315,7 @@ bool CheckUninitVar::isVariableUsage(const Scope* scope, const Token *vartok, bo return true; if (pointer && Token::Match(argStart, "%type% * %var% [,)]")) return true; - if (!(pointer && address) && Token::Match(argStart, "const %type% * %var% [,)]")) + if ((pointer || address) && Token::Match(argStart, "const %type% * %var% [,)]")) return true; } } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 75e3f27b3..3ad9e8856 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2387,52 +2387,76 @@ private: // Handling of function calls void uninitvar2_func() { - checkUninitVar2("void a(char c);\n" + // non-pointer variable + checkUninitVar2("void a(char c);\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(const char *s);\n" + checkUninitVar2("void a(const char c);\n" // const 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" // address => no error + "void b() {\n" + " char c;\n" + " a(&c);\n" + "}"); + ASSERT_EQUALS("", 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 *s);\n" + // pointer variable + checkUninitVar2("void a(char c);\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(const char c);\n" // const 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" // 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(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" " a(&c);\n" "}"); ASSERT_EQUALS("", errout.str()); - checkUninitVar2("void a(const ABC *abc);\n" + checkUninitVar2("void a(const char **c);\n" // const address of pointer => no error "void b() {\n" - " ABC abc;\n" - " a(&abc);\n" - "}"); - ASSERT_EQUALS("", errout.str()); - - checkUninitVar2("void a(const ABC *abc);\n" - "void b() {\n" - " ABC abc;\n" - " a(&abc);\n" - "}", "test.c"); - ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: abc\n", errout.str()); - - checkUninitVar2("void a(char *p);\n" - "void b() {\n" - " char *s;\n" - " a(s);\n" - "}"); - ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: s\n", errout.str()); - - checkUninitVar2("void a(const char **p);\n" - "void b() {\n" - " const char *s;\n" - " a(&s);\n" + " const char *c;\n" + " a(&c);\n" "}"); ASSERT_EQUALS("", errout.str()); }