Fix 11894: FPs knownArgument with sizeof and function pointer (#5396)

This commit is contained in:
Paul Fultz II 2023-09-02 07:28:54 -05:00 committed by GitHub
parent 37b2e50933
commit 6c0a5a5859
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -3764,11 +3764,17 @@ void CheckOther::checkKnownArgument()
mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, true, true))
continue;
// ensure that there is a integer variable in expression with unknown value
const Token* vartok = findAstNode(tok, [](const Token* child) {
const Token* vartok = nullptr;
visitAstNodes(tok, [&](const Token* child) {
if (Token::Match(child, "%var%|.|[")) {
return astIsIntegral(child, false) && !astIsPointer(child) && child->values().empty();
if (child->hasKnownIntValue())
return ChildrenToVisit::none;
if (astIsIntegral(child, false) && !astIsPointer(child) && child->values().empty()) {
vartok = child;
return ChildrenToVisit::done;
}
}
return false;
return ChildrenToVisit::op1_and_op2;
});
if (!vartok)
continue;

View File

@ -11033,6 +11033,18 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
// #11894
check("struct S {\n"
" int *p, n;\n"
"};\n"
"S* g() {\n"
" S* s = static_cast<S*>(calloc(1, sizeof(S)));\n"
" s->n = 100;\n"
" s->p = static_cast<int*>(malloc(s->n * sizeof(int)));\n"
" return s;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #11679
check("bool g(int);\n"
"void h(int);\n"