Skip to content

Commit 0f8db28

Browse files
IOBYTEdanmar
authored andcommitted
speed up checks by caching commonly looked up stuff in the symbol database (CheckBufferOverrun, CheckBoost)
1 parent d7c7f8c commit 0f8db28

2 files changed

Lines changed: 103 additions & 90 deletions

File tree

‎lib/checkboost.cpp‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "checkboost.h"
20+
#include "symboldatabase.h"
2021

2122
// Register this check class (by creating a static instance of it)
2223
namespace {
@@ -25,24 +26,29 @@ namespace {
2526

2627
void CheckBoost::checkBoostForeachModification()
2728
{
28-
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
29-
if (!Token::simpleMatch(tok, "BOOST_FOREACH ("))
30-
continue;
29+
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
30+
const std::size_t functions = symbolDatabase->functionScopes.size();
31+
for (std::size_t i = 0; i < functions; ++i) {
32+
const Scope * scope = symbolDatabase->functionScopes[i];
33+
for (const Token *tok = scope->classStart->next(); tok && tok != scope->classEnd; tok = tok->next()) {
34+
if (!Token::simpleMatch(tok, "BOOST_FOREACH ("))
35+
continue;
3136

32-
const Token *container_tok = tok->next()->link()->previous();
33-
if (!Token::Match(container_tok, "%var% ) {"))
34-
continue;
37+
const Token *container_tok = tok->next()->link()->previous();
38+
if (!Token::Match(container_tok, "%var% ) {"))
39+
continue;
3540

36-
const unsigned int container_id = container_tok->varId();
37-
if (container_id == 0)
38-
continue;
41+
const unsigned int container_id = container_tok->varId();
42+
if (container_id == 0)
43+
continue;
3944

40-
const Token *tok2 = container_tok->tokAt(2);
41-
const Token *end = tok2->link();
42-
for (; tok2 != end; tok2 = tok2->next()) {
43-
if (Token::Match(tok2, "%varid% . insert|erase|push_back|push_front|pop_front|pop_back|clear|swap|resize|assign|merge|remove|remove_if|reverse|sort|splice|unique|pop|push", container_id)) {
44-
boostForeachError(tok2);
45-
break;
45+
const Token *tok2 = container_tok->tokAt(2);
46+
const Token *end = tok2->link();
47+
for (; tok2 != end; tok2 = tok2->next()) {
48+
if (Token::Match(tok2, "%varid% . insert|erase|push_back|push_front|pop_front|pop_back|clear|swap|resize|assign|merge|remove|remove_if|reverse|sort|splice|unique|pop|push", container_id)) {
49+
boostForeachError(tok2);
50+
break;
51+
}
4652
}
4753
}
4854
}

‎lib/checkbufferoverrun.cpp‎

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,11 +1326,10 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
13261326
}
13271327
}
13281328

1329-
// find all dynamically allocated arrays next by parsing the token stream
1330-
// Count { and } when parsing all tokens
1331-
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
1332-
if (scope->type != Scope::eFunction)
1333-
continue;
1329+
// find all dynamically allocated arrays next
1330+
const std::size_t functions = symbolDatabase->functionScopes.size();
1331+
for (std::size_t i = 0; i < functions; ++i) {
1332+
const Scope * scope = symbolDatabase->functionScopes[i];
13341333

13351334
for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
13361335
// if the previous token exists, it must be either a variable name or "[;{}]"
@@ -1423,13 +1422,10 @@ void CheckBufferOverrun::checkStructVariable()
14231422
{
14241423
const SymbolDatabase * symbolDatabase = _tokenizer->getSymbolDatabase();
14251424

1426-
std::list<Scope>::const_iterator scope;
1427-
14281425
// find every class and struct
1429-
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
1430-
// only check classes and structures
1431-
if (!scope->isClassOrStruct())
1432-
continue;
1426+
const std::size_t classes = symbolDatabase->classAndStructScopes.size();
1427+
for (std::size_t i = 0; i < classes; ++i) {
1428+
const Scope * scope = symbolDatabase->classAndStructScopes[i];
14331429

14341430
// check all variables to see if they are arrays
14351431
std::list<Variable>::const_iterator var;
@@ -1439,13 +1435,10 @@ void CheckBufferOverrun::checkStructVariable()
14391435
// create ArrayInfo from the array variable
14401436
ArrayInfo arrayInfo(&*var, _tokenizer);
14411437

1442-
std::list<Scope>::const_iterator func_scope;
1443-
14441438
// find every function
1445-
for (func_scope = symbolDatabase->scopeList.begin(); func_scope != symbolDatabase->scopeList.end(); ++func_scope) {
1446-
// only check functions
1447-
if (func_scope->type != Scope::eFunction)
1448-
continue;
1439+
const std::size_t functions = symbolDatabase->functionScopes.size();
1440+
for (std::size_t j = 0; j < functions; ++j) {
1441+
const Scope * func_scope = symbolDatabase->functionScopes[j];
14491442

14501443
// If struct is declared in a function then check
14511444
// if scope_func matches
@@ -1570,8 +1563,8 @@ void CheckBufferOverrun::checkStructVariable()
15701563
ArrayInfo temp = arrayInfo;
15711564
temp.varid(0); // do variable lookup by variable and member names rather than varid
15721565
std::string varnames; // use class and member name for messages
1573-
for (unsigned int i = 0; i < varname.size(); ++i)
1574-
varnames += (i == 0 ? "" : ".") + varname[i];
1566+
for (unsigned int k = 0; k < varname.size(); ++k)
1567+
varnames += (k == 0 ? "" : ".") + varname[k];
15751568
temp.varname(varnames);
15761569
checkScope(CheckTok, varname, temp);
15771570
}
@@ -1734,44 +1727,50 @@ void CheckBufferOverrun::checkSprintfCall(const Token *tok, const MathLib::bigin
17341727
//---------------------------------------------------------------------------
17351728
void CheckBufferOverrun::checkBufferAllocatedWithStrlen()
17361729
{
1737-
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
1738-
unsigned int dstVarId;
1739-
unsigned int srcVarId;
1740-
1741-
// Look for allocation of a buffer based on the size of a string
1742-
if (Token::Match(tok, "%var% = malloc|g_malloc|g_try_malloc ( strlen ( %var% ) )")) {
1743-
dstVarId = tok->varId();
1744-
srcVarId = tok->tokAt(6)->varId();
1745-
tok = tok->tokAt(8);
1746-
} else if (Token::Match(tok, "%var% = new char [ strlen ( %var% ) ]")) {
1747-
dstVarId = tok->varId();
1748-
srcVarId = tok->tokAt(7)->varId();
1749-
tok = tok->tokAt(9);
1750-
} else if (Token::Match(tok, "%var% = realloc|g_realloc|g_try_realloc ( %var% , strlen ( %var% ) )")) {
1751-
dstVarId = tok->varId();
1752-
srcVarId = tok->tokAt(8)->varId();
1753-
tok = tok->tokAt(10);
1754-
} else
1755-
continue;
1730+
const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase();
17561731

1757-
// To avoid false positives and added complexity, we will only look for
1758-
// improper usage of the buffer within the block that it was allocated
1759-
for (const Token* const end = tok->scope()->classEnd; tok && tok->next() && tok != end; tok = tok->next()) {
1760-
// If the buffers are modified, we can't be sure of their sizes
1761-
if (tok->varId() == srcVarId || tok->varId() == dstVarId)
1762-
break;
1732+
const std::size_t functions = symbolDatabase->functionScopes.size();
1733+
for (std::size_t i = 0; i < functions; ++i) {
1734+
const Scope * scope = symbolDatabase->functionScopes[i];
1735+
for (const Token *tok = scope->classStart->next(); tok && tok != scope->classEnd; tok = tok->next()) {
1736+
unsigned int dstVarId;
1737+
unsigned int srcVarId;
1738+
1739+
// Look for allocation of a buffer based on the size of a string
1740+
if (Token::Match(tok, "%var% = malloc|g_malloc|g_try_malloc ( strlen ( %var% ) )")) {
1741+
dstVarId = tok->varId();
1742+
srcVarId = tok->tokAt(6)->varId();
1743+
tok = tok->tokAt(8);
1744+
} else if (Token::Match(tok, "%var% = new char [ strlen ( %var% ) ]")) {
1745+
dstVarId = tok->varId();
1746+
srcVarId = tok->tokAt(7)->varId();
1747+
tok = tok->tokAt(9);
1748+
} else if (Token::Match(tok, "%var% = realloc|g_realloc|g_try_realloc ( %var% , strlen ( %var% ) )")) {
1749+
dstVarId = tok->varId();
1750+
srcVarId = tok->tokAt(8)->varId();
1751+
tok = tok->tokAt(10);
1752+
} else
1753+
continue;
17631754

1764-
if (Token::Match(tok, "strcpy ( %varid% , %var% )", dstVarId) &&
1765-
tok->tokAt(4)->varId() == srcVarId) {
1766-
bufferOverrunError(tok);
1767-
} else if (Token::Match(tok, "sprintf ( %varid% , %str% , %var% )", dstVarId) &&
1768-
tok->tokAt(6)->varId() == srcVarId &&
1769-
tok->strAt(4).find("%s") != std::string::npos) {
1770-
bufferOverrunError(tok);
1755+
// To avoid false positives and added complexity, we will only look for
1756+
// improper usage of the buffer within the block that it was allocated
1757+
for (const Token* const end = tok->scope()->classEnd; tok && tok->next() && tok != end; tok = tok->next()) {
1758+
// If the buffers are modified, we can't be sure of their sizes
1759+
if (tok->varId() == srcVarId || tok->varId() == dstVarId)
1760+
break;
1761+
1762+
if (Token::Match(tok, "strcpy ( %varid% , %var% )", dstVarId) &&
1763+
tok->tokAt(4)->varId() == srcVarId) {
1764+
bufferOverrunError(tok);
1765+
} else if (Token::Match(tok, "sprintf ( %varid% , %str% , %var% )", dstVarId) &&
1766+
tok->tokAt(6)->varId() == srcVarId &&
1767+
tok->strAt(4).find("%s") != std::string::npos) {
1768+
bufferOverrunError(tok);
1769+
}
17711770
}
1771+
if (!tok)
1772+
return;
17721773
}
1773-
if (!tok)
1774-
return;
17751774
}
17761775
}
17771776

@@ -1790,8 +1789,11 @@ void CheckBufferOverrun::checkInsecureCmdLineArgs()
17901789
{
17911790
const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase();
17921791

1793-
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) {
1794-
for (std::list<Function>::const_iterator j = i->functionList.begin(); j != i->functionList.end(); ++j) {
1792+
std::size_t functions = symbolDatabase->functionScopes.size();
1793+
for (std::size_t i = 0; i < functions; ++i) {
1794+
const Scope * scope = symbolDatabase->functionScopes[i];
1795+
Function * j = scope->function;
1796+
if (j) {
17951797
const Token* tok = j->token;
17961798

17971799
// Get the name of the argv variable
@@ -1829,7 +1831,6 @@ void CheckBufferOverrun::checkInsecureCmdLineArgs()
18291831
tok->strAt(4).find("%s") != std::string::npos) {
18301832
cmdLineArgsError(tok);
18311833
}
1832-
18331834
}
18341835
}
18351836
}
@@ -2087,26 +2088,32 @@ void CheckBufferOverrun::arrayIndexThenCheck()
20872088
{
20882089
if (!_settings->isEnabled("style"))
20892090
return;
2090-
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
2091-
if (Token::Match(tok, "%var% [ %var% ]")) {
2092-
const std::string& indexName(tok->strAt(2));
2093-
2094-
// skip array index..
2095-
tok = tok->tokAt(4);
2096-
while (tok && tok->str() == "[")
2097-
tok = tok->link()->next();
20982091

2099-
// syntax error
2100-
if (!tok)
2101-
return;
2102-
2103-
// skip comparison
2104-
if (tok->type() == Token::eComparisonOp && tok->strAt(2) == "&&")
2105-
tok = tok->tokAt(2);
2106-
2107-
// check if array index is ok
2108-
if (Token::Match(tok, ("&& " + indexName + " <|<=").c_str()))
2109-
arrayIndexThenCheckError(tok, indexName);
2092+
const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase();
2093+
const std::size_t functions = symbolDatabase->functionScopes.size();
2094+
for (std::size_t i = 0; i < functions; ++i) {
2095+
const Scope * scope = symbolDatabase->functionScopes[i];
2096+
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
2097+
if (Token::Match(tok, "%var% [ %var% ]")) {
2098+
const std::string& indexName(tok->strAt(2));
2099+
2100+
// skip array index..
2101+
tok = tok->tokAt(4);
2102+
while (tok && tok->str() == "[")
2103+
tok = tok->link()->next();
2104+
2105+
// syntax error
2106+
if (!tok)
2107+
return;
2108+
2109+
// skip comparison
2110+
if (tok->type() == Token::eComparisonOp && tok->strAt(2) == "&&")
2111+
tok = tok->tokAt(2);
2112+
2113+
// check if array index is ok
2114+
if (Token::Match(tok, ("&& " + indexName + " <|<=").c_str()))
2115+
arrayIndexThenCheckError(tok, indexName);
2116+
}
21102117
}
21112118
}
21122119
}

0 commit comments

Comments
 (0)