Cache and reuse values

This commit is contained in:
Dmitry-Me 2017-09-05 17:50:36 +03:00
parent 27be75b224
commit 0e444aa133
1 changed files with 10 additions and 8 deletions

View File

@ -1664,23 +1664,25 @@ void CheckBufferOverrun::checkBufferAllocatedWithStrlen()
const Scope * scope = symbolDatabase->functionScopes[i]; const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token *tok = scope->classStart->next(); tok && tok != scope->classEnd; tok = tok->next()) { for (const Token *tok = scope->classStart->next(); tok && tok != scope->classEnd; tok = tok->next()) {
unsigned int dstVarId = tok->varId(); unsigned int dstVarId = tok->varId();
unsigned int srcVarId;
if (!dstVarId || tok->strAt(1) != "=") if (!dstVarId || tok->strAt(1) != "=")
continue; continue;
tok = tok->tokAt(2); tok = tok->tokAt(2);
unsigned int srcVarId;
// Look for allocation of a buffer based on the size of a string // Look for allocation of a buffer based on the size of a string
if (Token::Match(tok, "malloc|g_malloc|g_try_malloc|alloca ( strlen ( %var% ) )")) { if (Token::Match(tok, "malloc|g_malloc|g_try_malloc|alloca ( strlen ( %var% ) )")) {
srcVarId = tok->tokAt(4)->varId(); const Token *varTok = tok->tokAt(4);
tok = tok->tokAt(6); srcVarId = varTok->varId();
tok = varTok->tokAt(2);
} else if (_tokenizer->isCPP() && Token::Match(tok, "new char [ strlen ( %var% ) ]")) { } else if (_tokenizer->isCPP() && Token::Match(tok, "new char [ strlen ( %var% ) ]")) {
srcVarId = tok->tokAt(5)->varId(); const Token *varTok = tok->tokAt(5);
tok = tok->tokAt(7); srcVarId = varTok->varId();
tok = varTok->tokAt(2);
} else if (Token::Match(tok, "realloc|g_realloc|g_try_realloc ( %name% , strlen ( %var% ) )")) { } else if (Token::Match(tok, "realloc|g_realloc|g_try_realloc ( %name% , strlen ( %var% ) )")) {
srcVarId = tok->tokAt(6)->varId(); const Token *varTok = tok->tokAt(6);
tok = tok->tokAt(8); srcVarId = varTok->varId();
tok = varTok->tokAt(2);
} else } else
continue; continue;