From 6960332f8a31eaad16c5d2d0750a1ac19944134c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 6 Sep 2022 21:21:06 +0200 Subject: [PATCH] Fix #11294 FP arrayIndexOutOfBoundsCond with extra parentheses (#4442) --- lib/valueflow.cpp | 5 +++-- test/testvalueflow.cpp | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 9c36a28a8..a885e12b5 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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() && diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 4466a5903..8f1392aaa 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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" \