Merge pull request #166 from last5bits/ticket4213

Fixing #4213 arrayIndexThenCheck and adding tests
This commit is contained in:
Daniel Marjamäki 2013-08-23 22:36:30 -07:00
commit 5ce7189bc0
2 changed files with 22 additions and 2 deletions

View File

@ -2200,11 +2200,18 @@ void CheckBufferOverrun::arrayIndexThenCheck()
return; return;
// skip comparison // skip comparison
if (tok->type() == Token::eComparisonOp && tok->strAt(2) == "&&") if (tok->type() == Token::eComparisonOp)
tok = tok->tokAt(2); tok = tok->tokAt(2);
// skip close parenthesis
if(tok->str() == ")")
{
tok = tok->next();
}
// check if array index is ok // 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); arrayIndexThenCheckError(tok, indexName);
} }
} }

View File

@ -3885,6 +3885,19 @@ private:
" }" " }"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Array index 'i' is used before limits check.\n", errout.str()); 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() { void bufferNotZeroTerminated() {