diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index f42b9e9ed..e7b9a3804 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -2200,11 +2200,18 @@ void CheckBufferOverrun::arrayIndexThenCheck() return; // skip comparison - if (tok->type() == Token::eComparisonOp && tok->strAt(2) == "&&") + if (tok->type() == Token::eComparisonOp) tok = tok->tokAt(2); + // skip close parenthesis + if(tok->str() == ")") + { + tok = tok->next(); + } + // check if array index is ok - if (Token::Match(tok, ("&& " + indexName + " <|<=").c_str())) + // statement can be closed in parentheses, so "(| " is using + if (Token::Match(tok, ("&& (| " + indexName + " <|<=").c_str())) arrayIndexThenCheckError(tok, indexName); } } diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index da32670bc..45873cd47 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -3885,6 +3885,19 @@ private: " }" "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Array index 'i' is used before limits check.\n", errout.str()); + + check("void f(const int a[], unsigned i) {\n" + " if((a[i] < 2) && (i <= 42)) {\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (style) Array index 'i' is used before limits check.\n", errout.str()); + + // this one doesn't work for now, hopefully in the future + check("void f(const int a[], unsigned i) {\n" + " if(a[i] < func(i) && i <= 42) {\n" + " }\n" + "}"); + TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Array index 'i' is used before limits check.\n", "", errout.str()); } void bufferNotZeroTerminated() {