refactor CheckBufferOverrun::checkScope to take an ArrayInfo parameter

This commit is contained in:
Robert Reif 2011-09-10 11:21:52 -04:00
parent 794fdd056b
commit a9b4e21f60
2 changed files with 22 additions and 11 deletions

View File

@ -858,8 +858,17 @@ void CheckBufferOverrun::checkScopeForBody(const Token *tok, const ArrayInfo &ar
} }
void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::string> &varname, const MathLib::bigint size, const MathLib::bigint total_size, unsigned int varid) void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::string> &varname, const ArrayInfo &info)
{ {
// Only handling 1-dimensional arrays yet..
/** @todo false negatives: handle multi-dimension arrays someday */
if (info.num().size() > 1)
return;
const MathLib::bigint size = info.num(0);
const MathLib::bigint total_size = info.element_size() * info.num(0);
unsigned int varid = info.varid();
std::string varnames; std::string varnames;
for (unsigned int i = 0; i < varname.size(); ++i) for (unsigned int i = 0; i < varname.size(); ++i)
varnames += (i == 0 ? "" : " . ") + varname[i]; varnames += (i == 0 ? "" : " . ") + varname[i];
@ -1477,7 +1486,8 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
continue; continue;
std::vector<std::string> v; std::vector<std::string> v;
checkScope(tok->tokAt(nextTok), v, size, total_size, varid); ArrayInfo temp(varid, "", total_size / size, size);
checkScope(tok->tokAt(nextTok), v, temp);
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -1539,11 +1549,6 @@ void CheckBufferOverrun::checkStructVariable()
if (scope->nestedIn->isClassOrStruct()) if (scope->nestedIn->isClassOrStruct())
continue; continue;
// Only handling 1-dimensional arrays yet..
/** @todo false negatives: handle multi-dimension arrays someday */
if (arrayInfo.num().size() > 1)
continue;
std::vector<std::string> varname; std::vector<std::string> varname;
varname.push_back(""); varname.push_back("");
varname.push_back(arrayInfo.varname()); varname.push_back(arrayInfo.varname());
@ -1651,7 +1656,9 @@ void CheckBufferOverrun::checkStructVariable()
continue; continue;
// Check variable usage.. // Check variable usage..
checkScope(CheckTok, varname, static_cast<int>(arrayInfo.num(0)), static_cast<int>(arrayInfo.num(0) * arrayInfo.element_size()), 0); ArrayInfo temp = arrayInfo;
temp.varid(0);
checkScope(CheckTok, varname, temp);
} }
} }
} }

View File

@ -110,9 +110,6 @@ public:
/** Check for negative index */ /** Check for negative index */
void negativeIndex(); void negativeIndex();
/** Check for buffer overruns */
void checkScope(const Token *tok, const std::vector<std::string> &varname, const MathLib::bigint size, const MathLib::bigint total_size, unsigned int varid);
/** Information about N-dimensional array */ /** Information about N-dimensional array */
class ArrayInfo class ArrayInfo
{ {
@ -181,6 +178,10 @@ public:
{ {
return _varid; return _varid;
} }
void varid(unsigned int id)
{
_varid = id;
}
/** Variable name */ /** Variable name */
const std::string &varname() const const std::string &varname() const
@ -192,6 +193,9 @@ public:
/** Check for buffer overruns (based on ArrayInfo) */ /** Check for buffer overruns (based on ArrayInfo) */
void checkScope(const Token *tok, const ArrayInfo &arrayInfo); void checkScope(const Token *tok, const ArrayInfo &arrayInfo);
/** Check for buffer overruns */
void checkScope(const Token *tok, const std::vector<std::string> &varname, const ArrayInfo &arrayInfo);
/** Check scope helper function - parse for body */ /** Check scope helper function - parse for body */
void checkScopeForBody(const Token *tok, const ArrayInfo &arrayInfo, bool &bailout); void checkScopeForBody(const Token *tok, const ArrayInfo &arrayInfo, bool &bailout);