Refactorization: Avoid iterations over whole token list, limited several checks to function scopes.

This commit is contained in:
PKEuS 2014-10-31 11:40:42 +01:00
parent 5bc775e43e
commit 662283cab8
5 changed files with 251 additions and 220 deletions

View File

@ -172,7 +172,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
return No;
// is there a user function with this name?
if (tokenizer && Token::findmatch(tokenizer->tokens(), ("%type% *|&| " + tok2->str()).c_str()))
if (tok2->function())
return No;
return Fd;
}

View File

@ -21,6 +21,7 @@
//---------------------------------------------------------------------------
#include "checknonreentrantfunctions.h"
#include "symboldatabase.h"
//---------------------------------------------------------------------------
@ -35,21 +36,24 @@ void CheckNonReentrantFunctions::nonReentrantFunctions()
if (!_settings->standards.posix || !_settings->isEnabled("portability"))
return;
std::map<std::string,std::string>::const_iterator nonReentrant_end = _nonReentrantFunctions.end();
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase * const symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
// Look for function invocations
if (!tok->isName() || tok->strAt(1) != "(" || tok->varId() != 0)
if (tok->varId() != 0 || !tok->isName() || tok->strAt(1) != "(")
continue;
// Check for non-reentrant function name
std::map<std::string, std::string>::const_iterator it = _nonReentrantFunctions.find(tok->str());
if (it == nonReentrant_end)
if (it == _nonReentrantFunctions.end())
continue;
const Token *prev = tok->previous();
if (prev) {
// Ignore function definitions, class members or class definitions
if (prev->isName() || Token::Match(prev, ".|:"))
if (prev->str() == ".")
continue;
// Check for "std" or global namespace, ignore other namespaces
@ -61,4 +65,5 @@ void CheckNonReentrantFunctions::nonReentrantFunctions()
reportError(tok, Severity::portability, "nonreentrantFunctions" + it->first, it->second);
}
}
}
//---------------------------------------------------------------------------

View File

@ -998,8 +998,11 @@ void CheckOther::checkSuspiciousEqualityComparison()
if (!_settings->isEnabled("warning") || !_settings->inconclusive)
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
if (Token::simpleMatch(tok, "for (")) {
const Token* const openParen = tok->next();
const Token* const closeParen = tok->linkAt(1);
@ -1040,6 +1043,7 @@ void CheckOther::checkSuspiciousEqualityComparison()
}
}
}
}
void CheckOther::suspiciousEqualityComparisonError(const Token* tok)
{
@ -2027,7 +2031,12 @@ void CheckOther::duplicateBranchError(const Token *tok1, const Token *tok2)
void CheckOther::checkInvalidFree()
{
std::map<unsigned int, bool> allocatedVariables;
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
// Keep track of which variables were assigned addresses to newly-allocated memory
if (Token::Match(tok, "%var% = malloc|g_malloc|new")) {
@ -2083,6 +2092,7 @@ void CheckOther::checkInvalidFree()
}
}
}
}
void CheckOther::invalidFreeError(const Token *tok, bool inconclusive)
{
@ -2099,7 +2109,11 @@ void CheckOther::checkDoubleFree()
std::set<unsigned int> freedVariables;
std::set<unsigned int> closeDirVariables;
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
// Keep track of any variables passed to "free()", "g_free()" or "closedir()",
// and report an error if the same variable is passed twice.
if (Token::Match(tok, "free|g_free|closedir ( %var% )")) {
@ -2188,6 +2202,7 @@ void CheckOther::checkDoubleFree()
}
}
}
}
void CheckOther::doubleFreeError(const Token *tok, const std::string &varname)
{
@ -2375,10 +2390,16 @@ void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* to
//-----------------------------------------------------------------------------
void CheckOther::redundantGetAndSetUserId()
{
if (_settings->isEnabled("warning")
&& _settings->standards.posix) {
if (!_settings->standards.posix || !_settings->isEnabled("warning"))
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
// check all the code in the function
for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (Token::simpleMatch(tok, "setuid ( getuid ( ) )")
|| Token::simpleMatch(tok, "seteuid ( geteuid ( ) )")
|| Token::simpleMatch(tok, "setgid ( getgid ( ) )")

View File

@ -270,7 +270,11 @@ void CheckString::incorrectStringBooleanError(const Token *tok, const std::strin
//---------------------------------------------------------------------------
void CheckString::sprintfOverlappingData()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
// Get variable id of target buffer..
unsigned int varid = 0;
@ -301,6 +305,7 @@ void CheckString::sprintfOverlappingData()
} while (nullptr != (tok2 = tok2->nextArgument()));
}
}
}
void CheckString::sprintfOverlappingDataError(const Token *tok, const std::string &varname)
{

View File

@ -3682,20 +3682,20 @@ private:
}
void redundantGetAndSetUserId() {
check("seteuid(geteuid());\n", nullptr, false , false, true);
check("void foo() { seteuid(geteuid()); }", nullptr, false , false, true);
ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant get and set of user id.\n", errout.str());
check("setuid(getuid());\n", nullptr, false , false, true);
check("void foo() { setuid(getuid()); }", nullptr, false , false, true);
ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant get and set of user id.\n", errout.str());
check("setgid(getgid());\n", nullptr, false , false, true);
check("void foo() { setgid(getgid()); }", nullptr, false , false, true);
ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant get and set of user id.\n", errout.str());
check("setegid(getegid());\n", nullptr, false , false, true);
check("void foo() { setegid(getegid()); }", nullptr, false , false, true);
ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant get and set of user id.\n", errout.str());
check("seteuid(getuid());\n", nullptr, false , false, true);
check("void foo() { seteuid(getuid()); }", nullptr, false , false, true);
ASSERT_EQUALS("", errout.str());
check("seteuid(foo());\n", nullptr, false , false, true);
check("void foo() { seteuid(foo()); }", nullptr, false , false, true);
ASSERT_EQUALS("", errout.str());
check("foo(getuid());\n", nullptr, false , false, true);
check("void foo() { foo(getuid()); }", nullptr, false , false, true);
ASSERT_EQUALS("", errout.str());
}