buffer overflow: clean up old checking for negative index

This commit is contained in:
Daniel Marjamäki 2014-03-25 20:37:32 +01:00
parent 019d4491fb
commit 87daf5783e
3 changed files with 1 additions and 24 deletions

View File

@ -2038,26 +2038,6 @@ void CheckBufferOverrun::negativeIndexError(const Token *tok, MathLib::bigint in
reportError(tok, Severity::error, "negativeIndex", ostr.str()); reportError(tok, Severity::error, "negativeIndex", ostr.str());
} }
void CheckBufferOverrun::negativeIndex()
{
const char pattern[] = "[ %num% ]";
for (const Token *tok = Token::findmatch(_tokenizer->tokens(), pattern); tok; tok = Token::findmatch(tok->next(),pattern)) {
const MathLib::bigint index = MathLib::toLongNumber(tok->next()->str());
if (index < 0) {
// Negative index. Check if it's an array.
const Token *tok2 = tok;
while (tok2->strAt(-1) == "]")
tok2 = tok2->previous()->link();
if (tok2->previous() && tok2->previous()->varId()) {
const Variable *var = tok2->previous()->variable();
if (var && var->isArray())
negativeIndexError(tok, index);
}
}
}
}
CheckBufferOverrun::ArrayInfo::ArrayInfo() CheckBufferOverrun::ArrayInfo::ArrayInfo()
: _element_size(0), _declarationId(0) : _element_size(0), _declarationId(0)
{ {

View File

@ -62,7 +62,6 @@ public:
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) { void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckBufferOverrun checkBufferOverrun(tokenizer, settings, errorLogger); CheckBufferOverrun checkBufferOverrun(tokenizer, settings, errorLogger);
checkBufferOverrun.bufferOverrun(); checkBufferOverrun.bufferOverrun();
checkBufferOverrun.negativeIndex();
checkBufferOverrun.arrayIndexThenCheck(); checkBufferOverrun.arrayIndexThenCheck();
checkBufferOverrun.writeOutsideBufferSize(); checkBufferOverrun.writeOutsideBufferSize();
} }

View File

@ -67,7 +67,6 @@ private:
// Check for buffer overruns.. // Check for buffer overruns..
CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings, this); CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings, this);
checkBufferOverrun.bufferOverrun(); checkBufferOverrun.bufferOverrun();
checkBufferOverrun.negativeIndex();
checkBufferOverrun.arrayIndexThenCheck(); checkBufferOverrun.arrayIndexThenCheck();
checkBufferOverrun.writeOutsideBufferSize(); checkBufferOverrun.writeOutsideBufferSize();
} }
@ -1759,8 +1758,7 @@ private:
" TEST test;\n" " TEST test;\n"
" test.a[-1] = 3;\n" " test.a[-1] = 3;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Array 'test.a[10]' accessed at index -1, which is out of bounds.\n" ASSERT_EQUALS("[test.cpp:4]: (error) Array 'test.a[10]' accessed at index -1, which is out of bounds.\n", errout.str());
"[test.cpp:4]: (error) Array index -1 is out of bounds.\n", errout.str());
} }
void array_index_for_decr() { void array_index_for_decr() {