From 87daf5783e18947b6c61c9490ab83b8adadb5e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 25 Mar 2014 20:37:32 +0100 Subject: [PATCH] buffer overflow: clean up old checking for negative index --- lib/checkbufferoverrun.cpp | 20 -------------------- lib/checkbufferoverrun.h | 1 - test/testbufferoverrun.cpp | 4 +--- 3 files changed, 1 insertion(+), 24 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 39f05f2ec..fe5354f1f 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -2038,26 +2038,6 @@ void CheckBufferOverrun::negativeIndexError(const Token *tok, MathLib::bigint in 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() : _element_size(0), _declarationId(0) { diff --git a/lib/checkbufferoverrun.h b/lib/checkbufferoverrun.h index a8ba5db05..5f48540a4 100644 --- a/lib/checkbufferoverrun.h +++ b/lib/checkbufferoverrun.h @@ -62,7 +62,6 @@ public: void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) { CheckBufferOverrun checkBufferOverrun(tokenizer, settings, errorLogger); checkBufferOverrun.bufferOverrun(); - checkBufferOverrun.negativeIndex(); checkBufferOverrun.arrayIndexThenCheck(); checkBufferOverrun.writeOutsideBufferSize(); } diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 0f878228c..bf68491a1 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -67,7 +67,6 @@ private: // Check for buffer overruns.. CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings, this); checkBufferOverrun.bufferOverrun(); - checkBufferOverrun.negativeIndex(); checkBufferOverrun.arrayIndexThenCheck(); checkBufferOverrun.writeOutsideBufferSize(); } @@ -1759,8 +1758,7 @@ private: " TEST test;\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" - "[test.cpp:4]: (error) Array index -1 is out of bounds.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Array 'test.a[10]' accessed at index -1, which is out of bounds.\n", errout.str()); } void array_index_for_decr() {