Skip to content

Commit 169a22b

Browse files
Fix #13953 FP functionConst when casting address of pointer member (#7610)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 8b66b1c commit 169a22b

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/checkclass.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,6 +2360,14 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok)
23602360

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

2363+
static bool isNonConstPtrCast(const Token* tok)
2364+
{
2365+
if (!tok || !tok->isCast())
2366+
return false;
2367+
const ValueType* vt = tok->valueType();
2368+
return !vt || (vt->pointer > 0 && !vt->isConst(vt->pointer));
2369+
}
2370+
23632371
bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, MemberAccess& memberAccessed) const
23642372
{
23652373
if (mTokenizer->hasIfdef(func->functionScope->bodyStart, func->functionScope->bodyEnd))
@@ -2450,9 +2458,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
24502458
}
24512459

24522460
// non const pointer cast
2453-
if (tok1->valueType() && tok1->valueType()->pointer > 0 && tok1->astParent() && tok1->astParent()->isCast() &&
2454-
!(tok1->astParent()->valueType() &&
2455-
(tok1->astParent()->valueType()->pointer == 0 || tok1->astParent()->valueType()->isConst(tok1->astParent()->valueType()->pointer))))
2461+
if (tok1->valueType() && tok1->valueType()->pointer > 0 && isNonConstPtrCast(tok1->astParent()))
24562462
return false;
24572463

24582464
const Token* lhs = tok1->previous();
@@ -2463,6 +2469,12 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
24632469
else if (lhs->str() == ":" && lhs->astParent() && lhs->astParent()->astParent() && lhs->astParent()->str() == "?")
24642470
lhs = lhs->astParent()->astParent();
24652471
if (lhs->str() == "&") {
2472+
const Token* parent = lhs->astParent();
2473+
while (Token::Match(parent, "[+(]")) {
2474+
if (isNonConstPtrCast(parent))
2475+
return false;
2476+
parent = parent->astParent();
2477+
}
24662478
const Token* const top = lhs->astTop();
24672479
if (top->isAssignmentOp()) {
24682480
if (Token::simpleMatch(top->astOperand2(), "{") && !top->astOperand2()->previous()->function()) // TODO: check usage in init list

test/testclass.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class TestClass : public TestFixture {
189189
TEST_CASE(const96);
190190
TEST_CASE(const97);
191191
TEST_CASE(const98);
192+
TEST_CASE(const99);
192193

193194
TEST_CASE(const_handleDefaultParameters);
194195
TEST_CASE(const_passThisToMemberOfOtherClass);
@@ -6843,6 +6844,17 @@ class TestClass : public TestFixture {
68436844
ASSERT_EQUALS("", errout_str());
68446845
}
68456846

6847+
void const99() {
6848+
checkConst("typedef void (*InitFunc)(void**);\n" // #13953
6849+
"struct S {\n"
6850+
" int *m;\n"
6851+
" void f(InitFunc func) {\n"
6852+
" func(reinterpret_cast<void**>(&m));\n"
6853+
" }\n"
6854+
"};\n");
6855+
ASSERT_EQUALS("", errout_str());
6856+
}
6857+
68466858
void const_handleDefaultParameters() {
68476859
checkConst("struct Foo {\n"
68486860
" void foo1(int i, int j = 0) {\n"

0 commit comments

Comments
 (0)