Fix #11355 FN: arrayIndexOutOfBounds (#4555)

* Fix  #11355 FN: arrayIndexOutOfBounds

* Fix
This commit is contained in:
chrchr-github 2022-10-18 07:24:24 +02:00 committed by GitHub
parent 60d9a8fb54
commit 5a8e55c083
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 5 deletions

View File

@ -3159,6 +3159,26 @@ void Tokenizer::simplifyDoublePlusAndDoubleMinus()
void Tokenizer::arraySize()
{
auto getStrTok = [](Token* tok, bool addLength, Token** endStmt) -> Token* {
if (addLength) {
*endStmt = tok->tokAt(6);
return tok->tokAt(4);
}
if (Token::Match(tok, "%var% [ ] =")) {
tok = tok->tokAt(4);
int parCount = 0;
while (Token::simpleMatch(tok, "(")) {
++parCount;
tok = tok->next();
}
if (Token::Match(tok, "%str%")) {
*endStmt = tok->tokAt(parCount + 1);
return tok;
}
}
return nullptr;
};
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (!tok->isName() || !Token::Match(tok, "%var% [ ] ="))
continue;
@ -3170,11 +3190,11 @@ void Tokenizer::arraySize()
addlength = true;
}
if (addlength || Token::Match(tok, "%var% [ ] = %str% ;")) {
tok = tok->next();
const int sz = Token::getStrArraySize(tok->tokAt(3));
tok->insertToken(MathLib::toString(sz));
tok = tok->tokAt(5);
Token* endStmt{};
if (const Token* strTok = getStrTok(tok, addlength, &endStmt)) {
const int sz = Token::getStrArraySize(strTok);
tok->next()->insertToken(MathLib::toString(sz));
tok = endStmt;
}
else if (Token::Match(tok, "%var% [ ] = {")) {

View File

@ -194,6 +194,7 @@ private:
TEST_CASE(array_index_67); // #1596
TEST_CASE(array_index_68); // #6655
TEST_CASE(array_index_69); // #6370
TEST_CASE(array_index_70); // #11355
TEST_CASE(array_index_multidim);
TEST_CASE(array_index_switch_in_for);
TEST_CASE(array_index_for_in_for); // FP: #2634
@ -1901,6 +1902,15 @@ private:
ASSERT_EQUALS("[test.cpp:5]: (error) Array 'a[4]' accessed at index 30, which is out of bounds.\n", errout.str());
}
// #11355
void array_index_70() {
check("void f() {\n"
" static const char a[] = ((\"test\"));\n"
" printf(\"%c\", a[5]);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Array 'a[5]' accessed at index 5, which is out of bounds.\n", errout.str());
}
void array_index_multidim() {
check("void f()\n"
"{\n"