Fixing #4213 arrayIndexThenCheck and adding tests

This commit is contained in:
Alexey Zhikhartsev 2013-08-23 19:04:01 +04:00
parent 298cbd77ff
commit d24a321ba2
2 changed files with 22 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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() {