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;
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());
}
}
}

View File

@ -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 <stdio.h>\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 <stdio.h>\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"
);
}
//---------------------------------------------------------------------------

View File

@ -5958,6 +5958,14 @@ private:
" }\n"
"};");
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());
}