diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ba6a17b0e..58ffdbf4f 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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(); diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 613c62d8a..0f455c52f 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -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"