checkbufferoverrun.cpp: refactorizations

This commit is contained in:
PKEuS 2017-03-27 16:09:51 +02:00
parent 83b1e1c329
commit 63d39390b4
1 changed files with 23 additions and 24 deletions

View File

@ -456,9 +456,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &ftok, unsigned int
std::list<const Token *> callstack2(callstack); std::list<const Token *> callstack2(callstack);
callstack2.push_back(ftok2); callstack2.push_back(ftok2);
std::vector<MathLib::bigint> indexes; std::vector<MathLib::bigint> indexes(1, index);
indexes.push_back(index);
arrayIndexOutOfBoundsError(callstack2, arrayInfo, indexes); arrayIndexOutOfBoundsError(callstack2, arrayInfo, indexes);
} }
} }
@ -1027,7 +1025,7 @@ void CheckBufferOverrun::checkScope_inner(const Token *tok, const ArrayInfo &arr
MathLib::biguint charactersAppend = 0; MathLib::biguint charactersAppend = 0;
const Token *tok3 = tok2; const Token *tok3 = tok2;
while (Token::Match(tok3, "strcat ( %varid% , %str% ) ;", arrayInfo.declarationId())) { while (Token::Match(tok3, "strcat ( %varid% , %str% )", arrayInfo.declarationId())) {
charactersAppend += Token::getStrLength(tok3->tokAt(4)); charactersAppend += Token::getStrLength(tok3->tokAt(4));
if (charactersAppend >= total_size) { if (charactersAppend >= total_size) {
bufferOverrunError(tok3, arrayInfo.varname()); bufferOverrunError(tok3, arrayInfo.varname());
@ -1159,9 +1157,9 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
callstack.push_back(it->tokvalue); callstack.push_back(it->tokvalue);
callstack.push_back(tok); callstack.push_back(tok);
std::vector<MathLib::bigint> indexes2; std::vector<MathLib::bigint> indexes2(indexes.size());
for (unsigned int i = 0; i < indexes.size(); ++i) for (unsigned int i = 0; i < indexes.size(); ++i)
indexes2.push_back(indexes[i].intvalue); indexes2[i] = indexes[i].intvalue;
arrayIndexOutOfBoundsError(callstack, arrayInfo, indexes2); arrayIndexOutOfBoundsError(callstack, arrayInfo, indexes2);
} }
@ -1217,8 +1215,6 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
// size : Max array index // size : Max array index
MathLib::bigint size = 0; MathLib::bigint size = 0;
// varid : The variable id for the array
const Variable *var = nullptr;
// nextTok : used to skip to next statement. // nextTok : used to skip to next statement.
const Token * nextTok = tok; const Token * nextTok = tok;
@ -1230,31 +1226,34 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
if (_tokenizer->isMaxTime()) if (_tokenizer->isMaxTime())
return; return;
// varid : The variable id for the array
const Variable *var = tok->next()->variable();
if (_tokenizer->isCPP() && Token::Match(tok, "[*;{}] %var% = new %type% [")) { if (_tokenizer->isCPP() && Token::Match(tok, "[*;{}] %var% = new %type% [")) {
if (tok->tokAt(5)->astOperand2() == nullptr || tok->tokAt(5)->astOperand2()->getMaxValue(false) == nullptr) tok = tok->tokAt(5);
if (tok->astOperand2() == nullptr || tok->astOperand2()->getMaxValue(false) == nullptr)
continue; continue;
size = tok->tokAt(5)->astOperand2()->getMaxValue(false)->intvalue; size = tok->astOperand2()->getMaxValue(false)->intvalue;
var = tok->next()->variable(); nextTok = tok->link()->next();
nextTok = tok->linkAt(5)->next();
if (size < 0) { if (size < 0) {
negativeMemoryAllocationSizeError(tok->next()->next()); negativeMemoryAllocationSizeError(tok);
} }
} else if (_tokenizer->isCPP() && Token::Match(tok, "[*;{}] %var% = new %type% (|;")) { } else if (_tokenizer->isCPP() && Token::Match(tok, "[*;{}] %var% = new %type% (|;")) {
size = 1; size = 1;
var = tok->next()->variable(); tok = tok->tokAt(5);
if (tok->strAt(5) == ";") if (tok->str() == ";")
nextTok = tok->tokAt(6); nextTok = tok->next();
else else
nextTok = tok->linkAt(5)->next(); nextTok = tok->link()->next();
} else if (Token::Match(tok, "[*;{}] %var% = malloc|alloca (") && Token::simpleMatch(tok->linkAt(4), ") ;")) { } else if (Token::Match(tok, "[*;{}] %var% = malloc|alloca (") && Token::simpleMatch(tok->linkAt(4), ") ;")) {
if (tok->tokAt(4)->astOperand2() == nullptr || tok->tokAt(4)->astOperand2()->getMaxValue(false) == nullptr) tok = tok->tokAt(4);
if (tok->astOperand2() == nullptr || tok->astOperand2()->getMaxValue(false) == nullptr)
continue; continue;
size = tok->tokAt(4)->astOperand2()->getMaxValue(false)->intvalue; size = tok->astOperand2()->getMaxValue(false)->intvalue;
var = tok->next()->variable(); nextTok = tok->link()->tokAt(2);
nextTok = tok->linkAt(4)->tokAt(2);
if (size < 0) { if (size < 0) {
negativeMemoryAllocationSizeError(tok->next()->next()); negativeMemoryAllocationSizeError(tok);
} }
/** @todo false negatives: this may be too conservative */ /** @todo false negatives: this may be too conservative */
@ -1269,7 +1268,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
size /= static_cast<int>(typeSize); size /= static_cast<int>(typeSize);
} }
if (size < 0) { if (size < 0) {
negativeMemoryAllocationSizeError(tok->next()->next()); negativeMemoryAllocationSizeError(tok);
} }
} else { } else {
continue; continue;
@ -1282,7 +1281,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
if (totalSize == 0) if (totalSize == 0)
continue; continue;
ArrayInfo temp(var->declarationId(), tok->next()->str(), totalSize / size, size); ArrayInfo temp(var->declarationId(), var->name(), totalSize / size, size);
checkScope(nextTok, v, temp); checkScope(nextTok, v, temp);
} }
} }