From 8203c74c40b1c800612eafe32347601e54c27bad Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 13 Apr 2022 18:21:56 +0200 Subject: [PATCH] Fix #9648 FP sizeofDivisionMemfunc (#4017) * Fix #9648 FP sizeofDivisionMemfunc when result is multiplied again with sizeof later * Format --- lib/checksizeof.cpp | 13 +++++++++++++ test/testsizeof.cpp | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/checksizeof.cpp b/lib/checksizeof.cpp index 21a29f273..3c95a60ab 100644 --- a/lib/checksizeof.cpp +++ b/lib/checksizeof.cpp @@ -165,6 +165,19 @@ void CheckSizeof::checkSizeofForPointerSize() if (vt && vt->type == ValueType::CHAR && vt->pointer == 0) continue; } + auto hasMultiplication = [](const Token* parTok) -> bool { + while (parTok) { // Allow division if followed by multiplication + if (parTok->isArithmeticalOp() && parTok->str() == "*") { + for (const Token* szTok : { parTok->astOperand1(), parTok->astOperand2() }) + if (Token::simpleMatch(szTok, "(") && Token::simpleMatch(szTok->previous(), "sizeof")) + return true; + } + parTok = parTok->astParent(); + } + return false; + }; + if (hasMultiplication(tok2->astParent())) + continue; divideBySizeofError(tok2, tokFunc->str()); } diff --git a/test/testsizeof.cpp b/test/testsizeof.cpp index 77e19f3dc..b23f033c4 100644 --- a/test/testsizeof.cpp +++ b/test/testsizeof.cpp @@ -785,6 +785,14 @@ private: " strncpy(str, xyz, sizeof(str)/sizeof(str[0]));\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" // #9648 + " int a[5] = { 0 };\n" + " int b[5];\n" + " memcpy(b, a, ((sizeof(a) / sizeof(a[0])) - 1) * sizeof(a[0]));\n" + " memcpy(b, a, sizeof(a[0]) * ((sizeof(a) / sizeof(a[0])) - 1));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void sizeofVoid() {