From 7ec8adeb93bf58a3f5cb64a52f7f874f7f2afc5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 8 Aug 2015 08:53:08 +0200 Subject: [PATCH] Fixed #6701 (Uninitialized variable warning when header could not be found) --- lib/checkuninitvar.cpp | 2 +- test/testuninitvar.cpp | 99 ++++++++++++++++++++++++------------------ 2 files changed, 57 insertions(+), 44 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index d6b8f3d76..3d7e88795 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -82,7 +82,7 @@ void CheckUninitVar::checkScope(const Scope* scope) bool stdtype = _tokenizer->isC(); const Token* tok = i->typeStartToken(); - for (; tok && tok->str() != ";" && tok->str() != "<"; tok = tok->next()) { + for (; tok != i->nameToken() && tok->str() != "<"; tok = tok->next()) { if (tok->isStandardType()) stdtype = true; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index b9bf2bc6d..ca52252e6 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -16,9 +16,10 @@ * along with this program. If not, see . */ -#include "tokenize.h" -#include "checkuninitvar.h" #include "testsuite.h" +#include "checkuninitvar.h" +#include "tokenize.h" +#include "settings.h" class TestUninitVar : public TestFixture { @@ -33,6 +34,7 @@ private: LOAD_LIB_2(settings.library, "std.cfg"); TEST_CASE(uninitvar1); + TEST_CASE(uninitvar_decl); // handling various types in C and C++ files TEST_CASE(uninitvar_bitop); // using uninitialized operand in bit operation TEST_CASE(uninitvar_alloc); // data is allocated but not initialized TEST_CASE(uninitvar_arrays); // arrays @@ -54,7 +56,6 @@ private: TEST_CASE(uninitvar3); // #3844 TEST_CASE(uninitvar4); // #3869 (reference) TEST_CASE(uninitvar5); // #3861 - TEST_CASE(uninitvar6); // handling unknown types in C and C++ files TEST_CASE(uninitvar2_func); // function calls TEST_CASE(uninitvar2_value); // value flow TEST_CASE(uninitvar2_structmembers); // struct members @@ -645,6 +646,58 @@ private: ASSERT_EQUALS("", errout.str()); } + // Handling of unknown types. Assume they are POD in C. + void uninitvar_decl() { + const char code[] = "void f() {\n" + " dfs a;\n" + " return a;\n" + "}"; + + // Assume dfs is a non POD type if file is C++ + checkUninitVar(code, "test.cpp"); + ASSERT_EQUALS("", errout.str()); + + // Assume dfs is a POD type if file is C + checkUninitVar(code, "test.c"); + ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: a\n", errout.str()); + + const char code2[] = "struct AB { int a,b; };\n" + "void f() {\n" + " struct AB ab;\n" + " return ab;\n" + "}"; + checkUninitVar(code2, "test.cpp"); + ASSERT_EQUALS("", errout.str()); + checkUninitVar(code2, "test.c"); + ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab\n", errout.str()); + + // Ticket #3890 - False positive for std::map + checkUninitVar("void f() {\n" + " std::map x;\n" + " return x;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + // Ticket #3906 - False positive for std::vector pointer + checkUninitVar("void f() {\n" + " std::vector *x = NULL;\n" + " return x;\n" + "}", "test.cpp", false); + ASSERT_EQUALS("", errout.str()); + + // Ticket #6701 - Variable name is a POD type according to cfg + const char xmldata[] = "\n" + "" + " " + ""; + settings.library.loadxmldata(xmldata, sizeof(xmldata)); + checkUninitVar("void f() {\n" + " Fred _tm;\n" + " _tm.dostuff();\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void uninitvar3() { // #3844 // avoid false positive checkUninitVar("namespace std _GLIBCXX_VISIBILITY(default)\n" @@ -2554,20 +2607,6 @@ private: "}"); ASSERT_EQUALS("", errout.str()); - // Ticket #3890 - False positive for std::map - checkUninitVar("void f() {\n" - " std::map x;\n" - " return x;\n" - "}"); - ASSERT_EQUALS("", errout.str()); - - // Ticket #3906 - False positive for std::vector pointer - checkUninitVar("void f() {\n" - " std::vector *x = NULL;\n" - " return x;\n" - "}", "test.cpp", false); - ASSERT_EQUALS("", errout.str()); - // & checkUninitVar("void f() {\n" // #4426 - address of uninitialized variable " int a,b;\n" @@ -2628,32 +2667,6 @@ private: ASSERT_EQUALS("", errout.str()); } - // Handling of unknown types. Assume they are POD in C. - void uninitvar6() { - const char code[] = "void f() {\n" - " dfs a;\n" - " return a;\n" - "}"; - - // Assume dfs is a non POD type if file is C++ - checkUninitVar(code, "test.cpp"); - ASSERT_EQUALS("", errout.str()); - - // Assume dfs is a POD type if file is C - checkUninitVar(code, "test.c"); - ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: a\n", errout.str()); - - const char code2[] = "struct AB { int a,b; };\n" - "void f() {\n" - " struct AB ab;\n" - " return ab;\n" - "}"; - checkUninitVar(code2, "test.cpp"); - ASSERT_EQUALS("", errout.str()); - checkUninitVar(code2, "test.c"); - ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab\n", errout.str()); - } - void uninitvar7() { const char code[] = "void eDBauth_user() {\n" " char *blid_cert;\n"