From b0bf69bae7b6bff65aea685851d8a0fd32011728 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 14 Aug 2015 12:50:45 +0200 Subject: [PATCH] Fixed false positive #6763 and reordered conditions Ran AStyle --- lib/checkclass.cpp | 18 ++++++++++-------- lib/checkio.cpp | 44 ++++++++++++++++++++++---------------------- test/testclass.cpp | 8 ++++++++ 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index a273a9dbe..aef0b7825 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -815,18 +815,20 @@ void CheckClass::initializationListUsage() break; if (Token::Match(tok, "try|do {")) break; - if (Token::Match(tok, "%var% = %any%")) { + if (Token::Match(tok, "%var% = %any%") && tok->strAt(-1) != "*") { const Variable* var = tok->variable(); if (var && var->scope() == owner && !var->isStatic()) { + if (var->isPointer() || var->isReference() || (!var->type() && !var->isStlStringType() && !(Token::Match(var->typeStartToken(), "std :: %type% <") && !Token::simpleMatch(var->typeStartToken()->linkAt(3), "> ::")))) + continue; + bool allowed = true; for (const Token* tok2 = tok->tokAt(2); tok2 && tok2->str() != ";"; tok2 = tok2->next()) { - if (tok2->varId()) { - const Variable* var2 = tok2->variable(); - if (var2 && var2->scope() == owner && - tok2->strAt(-1)!=".") { // Is there a dependency between two member variables? + const Variable* var2 = tok2->variable(); + if (var2) { + if (var2->scope() == owner && tok2->strAt(-1)!=".") { // Is there a dependency between two member variables? allowed = false; break; - } else if (var2 && (var2->isArray() && var2->isLocal())) { // Can't initialize with a local array + } else if (var2->isArray() && var2->isLocal()) { // Can't initialize with a local array allowed = false; break; } @@ -840,8 +842,8 @@ void CheckClass::initializationListUsage() } if (!allowed) continue; - if (!var->isPointer() && !var->isReference() && (var->type() || var->isStlStringType() || (Token::Match(var->typeStartToken(), "std :: %type% <") && !Token::simpleMatch(var->typeStartToken()->linkAt(3), "> ::")))) - suggestInitializationList(tok, tok->str()); + + suggestInitializationList(tok, tok->str()); } } } diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 0d18df9cf..e6107b025 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -415,28 +415,28 @@ void CheckIO::invalidScanf() void CheckIO::invalidScanfError(const Token *tok) { - reportError(tok, Severity::warning, - "invalidscanf", "scanf without field width limits can crash with huge input data.\n" - "scanf without field width limits can crash with huge input data. Add a field width " - "specifier to fix this problem:\n" - " %s => %20s\n" - "\n" - "Sample program that can crash:\n" - "\n" - "#include \n" - "int main()\n" - "{\n" - " char c[5];\n" - " scanf(\"%s\", c);\n" - " return 0;\n" - "}\n" - "\n" - "Typing in 5 or more characters may make the program crash. The correct usage " - "here is 'scanf(\"%4s\", c);', as the maximum field width does not include the " - "terminating null byte.\n" - "Source: http://linux.die.net/man/3/scanf\n" - "Source: http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/libkern/stdio/scanf.c" - ); + reportError(tok, Severity::warning, + "invalidscanf", "scanf without field width limits can crash with huge input data.\n" + "scanf without field width limits can crash with huge input data. Add a field width " + "specifier to fix this problem:\n" + " %s => %20s\n" + "\n" + "Sample program that can crash:\n" + "\n" + "#include \n" + "int main()\n" + "{\n" + " char c[5];\n" + " scanf(\"%s\", c);\n" + " return 0;\n" + "}\n" + "\n" + "Typing in 5 or more characters may make the program crash. The correct usage " + "here is 'scanf(\"%4s\", c);', as the maximum field width does not include the " + "terminating null byte.\n" + "Source: http://linux.die.net/man/3/scanf\n" + "Source: http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/libkern/stdio/scanf.c" + ); } //--------------------------------------------------------------------------- diff --git a/test/testclass.cpp b/test/testclass.cpp index 62aa97f53..47025d714 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -5958,6 +5958,14 @@ private: " }\n" "};"); ASSERT_EQUALS("", errout.str()); + + checkInitializationListUsage("class B {\n" // #5640 + " std::shared_ptr _d;\n" + " B(const B& other) : _d(std::make_shared()) {\n" + " *_d = *other._d;\n" + " }\n" + "};"); + ASSERT_EQUALS("", errout.str()); }