diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 2f7b1e2ac..43a73686f 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -48,17 +48,17 @@ CheckBufferOverrun instance; void CheckBufferOverrun::arrayIndexOutOfBounds(const Token *tok, int size, int index) { - if (size == 1) - return; - - 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()); + 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::arrayIndexOutOfBounds(const Token *tok, const ArrayInfo &arrayInfo, const std::vector &index) @@ -780,17 +780,6 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo } - // in case %var% is declared as a pointer - else if (Token::Match(tok, "%var% [ %num% ]")) - { - const int index = MathLib::toLongNumber(tok->strAt(2)); - if (index < 0) - { - arrayIndexOutOfBounds(tok, index, index); - } - - } - // Loop.. else if (Token::simpleMatch(tok, "for (")) { diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index ee90a24e0..b0facd6f8 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -943,7 +943,8 @@ private: " int *ip = &i[1];\n" " ip[-10] = 1;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5]: (error) Array 'ip[-10]' index -10 out of bounds\n", errout.str()); + ASSERT_EQUALS("", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Array ip[-10] out of bounds\n", errout.str()); } void array_index_multidim() @@ -1118,6 +1119,14 @@ private: " p[-1] = 0;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void foo()\n" + "{\n" + " char s[] = \"abc\";\n" + " char *p = s + strlen(s);\n" + " if (p[-1]);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void array_index_for_decr()