Skip to content

Fix #13953 FP functionConst when casting address of pointer member #7610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,14 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok)

const std::set<std::string> CheckClass::stl_containers_not_const = { "map", "unordered_map", "std :: map|unordered_map <" }; // start pattern

static bool isNonConstPtrCast(const Token* tok)
{
if (!tok || !tok->isCast())
return false;
const ValueType* vt = tok->valueType();
return !vt || (vt->pointer > 0 && !vt->isConst(vt->pointer));
}

bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, MemberAccess& memberAccessed) const
{
if (mTokenizer->hasIfdef(func->functionScope->bodyStart, func->functionScope->bodyEnd))
Expand Down Expand Up @@ -2445,9 +2453,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
}

// non const pointer cast
if (tok1->valueType() && tok1->valueType()->pointer > 0 && tok1->astParent() && tok1->astParent()->isCast() &&
!(tok1->astParent()->valueType() &&
(tok1->astParent()->valueType()->pointer == 0 || tok1->astParent()->valueType()->isConst(tok1->astParent()->valueType()->pointer))))
if (tok1->valueType() && tok1->valueType()->pointer > 0 && isNonConstPtrCast(tok1->astParent()))
return false;

const Token* lhs = tok1->previous();
Expand All @@ -2458,6 +2464,12 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
else if (lhs->str() == ":" && lhs->astParent() && lhs->astParent()->astParent() && lhs->astParent()->str() == "?")
lhs = lhs->astParent()->astParent();
if (lhs->str() == "&") {
const Token* parent = lhs->astParent();
while (Token::Match(parent, "[+(]")) {
if (isNonConstPtrCast(parent))
return false;
parent = parent->astParent();
}
const Token* const top = lhs->astTop();
if (top->isAssignmentOp()) {
if (Token::simpleMatch(top->astOperand2(), "{") && !top->astOperand2()->previous()->function()) // TODO: check usage in init list
Expand Down
12 changes: 12 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class TestClass : public TestFixture {
TEST_CASE(const96);
TEST_CASE(const97);
TEST_CASE(const98);
TEST_CASE(const99);

TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass);
Expand Down Expand Up @@ -6835,6 +6836,17 @@ class TestClass : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void const99() {
checkConst("typedef void (*InitFunc)(void**);\n" // #13953
"struct S {\n"
" int *m;\n"
" void f(InitFunc func) {\n"
" func(reinterpret_cast<void**>(&m));\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}

void const_handleDefaultParameters() {
checkConst("struct Foo {\n"
" void foo1(int i, int j = 0) {\n"
Expand Down
Loading