Fix #10979 FP arrayIndexOutOfBoundsCond with sizeof (#4021)

* Fix #10466 FP constVariable with pointer typedef

* Fix flag check

* Use isStatic()

* Fix #10979 FP arrayIndexOutOfBoundsCond with sizeof and extra parentheses
This commit is contained in:
chrchr-github 2022-04-14 18:13:29 +02:00 committed by GitHub
parent a44cbb75ec
commit 6450d5701c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -1091,14 +1091,14 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
} else if (Token::Match(tok, "sizeof ( %var% ) / sizeof (") && tok->next()->astParent() == tok->tokAt(4)) {
// Get number of elements in array
const Token *sz1 = tok->tokAt(2);
const Token *sz2 = tok->tokAt(7);
const Token *sz2 = tok->tokAt(6); // left parenthesis
const nonneg int varid1 = sz1->varId();
if (varid1 &&
sz1->variable() &&
sz1->variable()->isArray() &&
!sz1->variable()->dimensions().empty() &&
sz1->variable()->dimensionKnown(0) &&
(Token::Match(sz2, "* %varid% )", varid1) || Token::Match(sz2, "%varid% [ 0 ] )", varid1))) {
Token::Match(sz2->astOperand2(), "*|[") && Token::Match(sz2->astOperand2()->astOperand1(), "%var%", varid1)) {
ValueFlow::Value value(sz1->variable()->dimension(0));
if (!tok2->isTemplateArg() && settings->platformType != cppcheck::Platform::Unspecified)
value.setKnown();

View File

@ -187,6 +187,7 @@ private:
TEST_CASE(array_index_60); // #10617, #9824
TEST_CASE(array_index_61); // #10621
TEST_CASE(array_index_62); // #7684
TEST_CASE(array_index_63); // #10979
TEST_CASE(array_index_multidim);
TEST_CASE(array_index_switch_in_for);
TEST_CASE(array_index_for_in_for); // FP: #2634
@ -1782,6 +1783,18 @@ private:
errout.str());
}
void array_index_63()
{
check("int b[4];\n" // #10979
"void f(int i) {\n"
" if (i >= 0 && i < sizeof(b) / sizeof(*(b)))\n"
" b[i] = 0;\n"
" if (i >= 0 && i < sizeof(b) / sizeof((b)[0]))\n"
" b[i] = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void array_index_multidim() {
check("void f()\n"
"{\n"