Fix #9648 FP sizeofDivisionMemfunc (#4017)

* Fix #9648 FP sizeofDivisionMemfunc when result is multiplied again with sizeof later

* Format
This commit is contained in:
chrchr-github 2022-04-13 18:21:56 +02:00 committed by GitHub
parent 938517b80a
commit 8203c74c40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -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());
}

View File

@ -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() {