Skip to content

Commit 6960332

Browse files
Fix #11294 FP arrayIndexOutOfBoundsCond with extra parentheses (#4442)
1 parent b3762cd commit 6960332

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

lib/valueflow.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,10 +1164,11 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
11641164
value.setKnown();
11651165
setTokenValue(tok, value, settings);
11661166
setTokenValue(tok->next(), value, settings);
1167-
} else if (Token::Match(tok, "sizeof ( %var% ) / sizeof (") && tok->next()->astParent() == tok->tokAt(4)) {
1167+
} else if (Token::Match(tok, "sizeof ( %var% ) /") && tok->next()->astParent() == tok->tokAt(4) &&
1168+
tok->tokAt(4)->astOperand2() && Token::simpleMatch(tok->tokAt(4)->astOperand2()->previous(), "sizeof (")) {
11681169
// Get number of elements in array
11691170
const Token *sz1 = tok->tokAt(2);
1170-
const Token *sz2 = tok->tokAt(6); // left parenthesis
1171+
const Token *sz2 = tok->tokAt(4)->astOperand2(); // left parenthesis of sizeof on rhs
11711172
const nonneg int varid1 = sz1->varId();
11721173
if (varid1 &&
11731174
sz1->variable() &&

test/testvalueflow.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,15 @@ class TestValueFlow : public TestFixture {
11941194
ASSERT_EQUALS(1U, values.size());
11951195
ASSERT_EQUALS(10, values.back().intvalue);
11961196

1197+
code = "void f() {\n" // #11294
1198+
" struct S { int i; };\n"
1199+
" const S a[] = { 1, 2 };\n"
1200+
" x = sizeof(a) / ( sizeof(a[0]) );\n"
1201+
"}";
1202+
values = tokenValues(code, "/");
1203+
ASSERT_EQUALS(1U, values.size());
1204+
ASSERT_EQUALS(2, values.back().intvalue);
1205+
11971206
#define CHECK(A, B, C, D) \
11981207
do { \
11991208
code = "enum " A " E " B " { E0, E1 };\n" \

0 commit comments

Comments
 (0)