Fixed false positive #6763 and reordered conditions

Ran AStyle
This commit is contained in:
PKEuS 2015-08-14 12:50:45 +02:00
parent 3cf67014ad
commit b0bf69bae7
3 changed files with 40 additions and 30 deletions

View File

@ -815,18 +815,20 @@ void CheckClass::initializationListUsage()
break; break;
if (Token::Match(tok, "try|do {")) if (Token::Match(tok, "try|do {"))
break; break;
if (Token::Match(tok, "%var% = %any%")) { if (Token::Match(tok, "%var% = %any%") && tok->strAt(-1) != "*") {
const Variable* var = tok->variable(); const Variable* var = tok->variable();
if (var && var->scope() == owner && !var->isStatic()) { 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; bool allowed = true;
for (const Token* tok2 = tok->tokAt(2); tok2 && tok2->str() != ";"; tok2 = tok2->next()) { for (const Token* tok2 = tok->tokAt(2); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->varId()) { const Variable* var2 = tok2->variable();
const Variable* var2 = tok2->variable(); if (var2) {
if (var2 && var2->scope() == owner && if (var2->scope() == owner && tok2->strAt(-1)!=".") { // Is there a dependency between two member variables?
tok2->strAt(-1)!=".") { // Is there a dependency between two member variables?
allowed = false; allowed = false;
break; 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; allowed = false;
break; break;
} }
@ -840,8 +842,8 @@ void CheckClass::initializationListUsage()
} }
if (!allowed) if (!allowed)
continue; 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());
} }
} }
} }

View File

@ -415,28 +415,28 @@ void CheckIO::invalidScanf()
void CheckIO::invalidScanfError(const Token *tok) void CheckIO::invalidScanfError(const Token *tok)
{ {
reportError(tok, Severity::warning, reportError(tok, Severity::warning,
"invalidscanf", "scanf without field width limits can crash with huge input data.\n" "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 " "scanf without field width limits can crash with huge input data. Add a field width "
"specifier to fix this problem:\n" "specifier to fix this problem:\n"
" %s => %20s\n" " %s => %20s\n"
"\n" "\n"
"Sample program that can crash:\n" "Sample program that can crash:\n"
"\n" "\n"
"#include <stdio.h>\n" "#include <stdio.h>\n"
"int main()\n" "int main()\n"
"{\n" "{\n"
" char c[5];\n" " char c[5];\n"
" scanf(\"%s\", c);\n" " scanf(\"%s\", c);\n"
" return 0;\n" " return 0;\n"
"}\n" "}\n"
"\n" "\n"
"Typing in 5 or more characters may make the program crash. The correct usage " "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 " "here is 'scanf(\"%4s\", c);', as the maximum field width does not include the "
"terminating null byte.\n" "terminating null byte.\n"
"Source: http://linux.die.net/man/3/scanf\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" "Source: http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/libkern/stdio/scanf.c"
); );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -5958,6 +5958,14 @@ private:
" }\n" " }\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkInitializationListUsage("class B {\n" // #5640
" std::shared_ptr<A> _d;\n"
" B(const B& other) : _d(std::make_shared<A>()) {\n"
" *_d = *other._d;\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
} }