diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 1cbb5609c..3078812fd 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 9cebee125..ddfded43d 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -3876,6 +3876,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() {