Fix #11294 FP arrayIndexOutOfBoundsCond with extra parentheses (#4442)

This commit is contained in:
chrchr-github 2022-09-06 21:21:06 +02:00 committed by GitHub
parent b3762cd76a
commit 6960332f8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -1164,10 +1164,11 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
value.setKnown();
setTokenValue(tok, value, settings);
setTokenValue(tok->next(), value, settings);
} else if (Token::Match(tok, "sizeof ( %var% ) / sizeof (") && tok->next()->astParent() == tok->tokAt(4)) {
} else if (Token::Match(tok, "sizeof ( %var% ) /") && tok->next()->astParent() == tok->tokAt(4) &&
tok->tokAt(4)->astOperand2() && Token::simpleMatch(tok->tokAt(4)->astOperand2()->previous(), "sizeof (")) {
// Get number of elements in array
const Token *sz1 = tok->tokAt(2);
const Token *sz2 = tok->tokAt(6); // left parenthesis
const Token *sz2 = tok->tokAt(4)->astOperand2(); // left parenthesis of sizeof on rhs
const nonneg int varid1 = sz1->varId();
if (varid1 &&
sz1->variable() &&

View File

@ -1194,6 +1194,15 @@ private:
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(10, values.back().intvalue);
code = "void f() {\n" // #11294
" struct S { int i; };\n"
" const S a[] = { 1, 2 };\n"
" x = sizeof(a) / ( sizeof(a[0]) );\n"
"}";
values = tokenValues(code, "/");
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(2, values.back().intvalue);
#define CHECK(A, B, C, D) \
do { \
code = "enum " A " E " B " { E0, E1 };\n" \