Fixing #4213 arrayIndexThenCheck and adding tests
This commit is contained in:
parent
298cbd77ff
commit
d24a321ba2
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3876,6 +3876,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() {
|
||||||
|
|
Loading…
Reference in New Issue