refactor CheckBufferOverrun to only use multi-dimension array error messages and remove single dimension array message

This commit is contained in:
Robert Reif 2011-09-11 09:54:26 -04:00
parent 55230baf78
commit e18fe56d56
2 changed files with 25 additions and 25 deletions

View File

@ -47,21 +47,6 @@ CheckBufferOverrun instance;
//---------------------------------------------------------------------------
void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, MathLib::bigint size, MathLib::bigint index)
{
if (size >= 1)
{
std::ostringstream errmsg;
errmsg << "Array '";
if (tok)
errmsg << tok->str();
else
errmsg << "array";
errmsg << "[" << size << "]' index " << index << " out of bounds";
reportError(tok, Severity::error, "arrayIndexOutOfBounds", errmsg.str().c_str());
}
}
void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index)
{
std::ostringstream oss;
@ -557,11 +542,15 @@ void CheckBufferOverrun::parse_for_body(const Token *tok2, const ArrayInfo &arra
//printf("min_index = %d, max_index = %d, size = %d\n", min_index, max_index, size);
if (min_index < 0 || max_index < 0)
{
arrayIndexOutOfBoundsError(tok2, (int)arrayInfo.num(0), std::min(min_index, max_index));
std::vector<MathLib::bigint> indexes;
indexes.push_back(std::min(min_index, max_index));
arrayIndexOutOfBoundsError(tok2, arrayInfo, indexes);
}
if (min_index >= (int)arrayInfo.num(0) || max_index >= (int)arrayInfo.num(0))
{
arrayIndexOutOfBoundsError(tok2, (int)arrayInfo.num(0), std::max(min_index, max_index));
std::vector<MathLib::bigint> indexes;
indexes.push_back(std::max(min_index, max_index));
arrayIndexOutOfBoundsError(tok2, arrayInfo, indexes);
}
}
}
@ -890,7 +879,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(2));
if (index >= size)
{
arrayIndexOutOfBoundsError(tok, size, index);
std::vector<MathLib::bigint> indexes;
indexes.push_back(index);
arrayIndexOutOfBoundsError(tok, info, indexes);
}
}
}
@ -899,7 +890,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(2 + varc));
if (index >= size)
{
arrayIndexOutOfBoundsError(tok->tokAt(varc), size, index);
std::vector<MathLib::bigint> indexes;
indexes.push_back(index);
arrayIndexOutOfBoundsError(tok->tokAt(varc), info, indexes);
}
}
@ -945,7 +938,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
{
if (index > size || !Token::simpleMatch(tok->previous(), "& ("))
{
arrayIndexOutOfBoundsError(tok->next(), size, index);
std::vector<MathLib::bigint> indexes;
indexes.push_back(index);
arrayIndexOutOfBoundsError(tok->next(), info, indexes);
}
}
}
@ -954,7 +949,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(3));
if (index < 0 || index >= size)
{
arrayIndexOutOfBoundsError(tok->next(), size, index);
std::vector<MathLib::bigint> indexes;
indexes.push_back(index);
arrayIndexOutOfBoundsError(tok->next(), info, indexes);
}
}
}
@ -963,7 +960,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(3 + varc));
if (index >= size)
{
arrayIndexOutOfBoundsError(tok->tokAt(1 + varc), size, index);
std::vector<MathLib::bigint> indexes;
indexes.push_back(index);
arrayIndexOutOfBoundsError(tok->tokAt(1 + varc), info, indexes);
}
tok = tok->tokAt(4);
continue;
@ -1486,7 +1485,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
continue;
std::vector<std::string> v;
ArrayInfo temp(varid, "", total_size / size, size);
ArrayInfo temp(varid, tok->next()->str(), total_size / size, size);
checkScope(tok->tokAt(nextTok), v, temp);
}
}

View File

@ -217,7 +217,6 @@ public:
*/
void checkFunctionCall(const Token *tok, const ArrayInfo &arrayInfo);
void arrayIndexOutOfBoundsError(const Token *tok, MathLib::bigint size, MathLib::bigint index);
void arrayIndexOutOfBoundsError(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index);
void arrayIndexOutOfBoundsError(const std::list<const Token *> &callstack, const ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index);
void bufferOverrunError(const Token *tok, const std::string &varnames = "");
@ -235,7 +234,9 @@ public:
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
CheckBufferOverrun c(0, settings, errorLogger);
c.arrayIndexOutOfBoundsError(0, 2, 2);
std::vector<MathLib::bigint> indexes;
indexes.push_back(2);
c.arrayIndexOutOfBoundsError(0, ArrayInfo(0, "array", 1, 2), indexes);
c.bufferOverrunError(0, std::string("buffer"));
c.strncatUsageError(0);
c.outOfBoundsError(0, "index");