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" \