From 3e23e243f6f2329a3bc4637d8ffb2cc283dc8bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 17 Jan 2014 19:44:45 +0100 Subject: [PATCH] BufferOverflow: Updated message for out of bounds array index or redundant condition --- lib/checkbufferoverrun.cpp | 13 +++++++++---- test/testbufferoverrun.cpp | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 844d53e54..50e12855a 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -70,10 +70,15 @@ void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const Arra errmsg << "Array '" << arrayInfo.varname() << "[" << arrayInfo.num(0) << "]' accessed at index " << index.intvalue << ", which is out of bounds."; - if (index.condition) + if (index.condition) { errmsg << " Otherwise condition '" << index.condition->expressionString() << "' is redundant."; - - reportError(tok, Severity::error, "arrayIndexOutOfBounds", errmsg.str()); + std::list callstack; + callstack.push_back(tok); + callstack.push_back(index.condition); + reportError(callstack, Severity::warning, "arrayIndexOutOfBoundsCond", errmsg.str()); + } else { + reportError(tok, Severity::error, "arrayIndexOutOfBounds", errmsg.str()); + } } void CheckBufferOverrun::arrayIndexOutOfBoundsError(const std::list &callstack, const ArrayInfo &arrayInfo, const std::vector &index) @@ -1155,7 +1160,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo const std::list &values = tok->next()->astOperand2()->values; std::list::const_iterator it; for (it = values.begin(); it != values.end(); ++it) { - if (it->intvalue >= arrayInfo.num()[0]) { + if (it->intvalue >= arrayInfo.num()[0] && (_settings->isEnabled("warning") || !it->condition)) { arrayIndexOutOfBoundsError(tok, arrayInfo, *it); } } diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 53efeb765..fcdf7184d 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -2062,7 +2062,7 @@ private: " str[i] = 0;\n" " if (i==10) {}\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) Array 'str[3]' accessed at index 10, which is out of bounds. Otherwise condition 'i==10' is redundant.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Array 'str[3]' accessed at index 10, which is out of bounds. Otherwise condition 'i==10' is redundant.\n", errout.str()); } void buffer_overrun_1_standard_functions() {