Skip to content

Commit 84f55fb

Browse files
dmcppcheckclaude
andcommitted
Fix #15304 (crash in exprDependsOnThis: using-declaration replaces injected-class-name inside class definition)
`using N::C;` caused simplifyUsing to replace `C` inside the definition of class N::C, e.g. `~C()` became `~N::C()`. The destructor was then not recognized and its body was parsed as class members, creating bogus function declarations whose arguments have no scope. exprDependsOnThis dereferenced that null scope. Seen when checking quickshell-0.3.2/src/services/polkit/listener.cpp. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6a4fedd commit 84f55fb

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎lib/astutils.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth)
11601160
}
11611161
if (expr->isOperatorKeyword() && !Token::simpleMatch(expr->next()->astParent(), "."))
11621162
return true;
1163-
if (expr->variable() && expr->variable()->isArgument() && !expr->variable()->type() && expr->variable()->scope()->function && expr->variable()->scope()->function->templateDef)
1163+
if (expr->variable() && expr->variable()->isArgument() && !expr->variable()->type() && expr->variable()->scope() && expr->variable()->scope()->function && expr->variable()->scope()->function->templateDef)
11641164
return true;
11651165
}
11661166
if (onVar && expr->variable()) {

‎lib/tokenize.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,18 @@ namespace {
28202820
tok2 = tok2->tokAt(-2);
28212821
}
28222822

2823+
// Inside a class the unqualified class name is the injected-class-name,
2824+
// it hides any type alias with the same name declared outside the class.
2825+
if (qualification.empty() && tok1->strAt(-1) != "::") {
2826+
for (const ScopeInfo3 *s = memberClassScope ? memberClassScope : currentScope; s; s = s->parent) {
2827+
if (s->type != ScopeInfo3::Record || s->name != tok1->str())
2828+
continue;
2829+
if (scope != s->fullName && scope.compare(0, s->fullName.size() + 4, s->fullName + " :: ") != 0)
2830+
return false;
2831+
break;
2832+
}
2833+
}
2834+
28232835
std::string fullScope1 = scope1;
28242836
if (!scope1.empty() && !qualification.empty())
28252837
fullScope1 += " :: ";

‎test/testsimplifyusing.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class TestSimplifyUsing : public TestFixture {
8080
TEST_CASE(simplifyUsing40);
8181
TEST_CASE(simplifyUsing41);
8282
TEST_CASE(simplifyUsing42);
83+
TEST_CASE(simplifyUsing43);
8384

8485
TEST_CASE(simplifyUsing8970);
8586
TEST_CASE(simplifyUsing8971);
@@ -991,6 +992,31 @@ class TestSimplifyUsing : public TestFixture {
991992
}
992993
}
993994

995+
void simplifyUsing43() {
996+
// injected-class-name must not be replaced
997+
const char code[] = "namespace N {\n"
998+
"class C {\n"
999+
" C(int* p) : p(p) {}\n"
1000+
" ~C() { unref(p); }\n"
1001+
" C& operator=(const C& other);\n"
1002+
" int* p;\n"
1003+
"};\n"
1004+
"}\n"
1005+
"using N::C;\n"
1006+
"C c;\n";
1007+
const char expected[] = "namespace N { "
1008+
"class C { "
1009+
"C ( int * p ) : p ( p ) { } "
1010+
"~ C ( ) { unref ( p ) ; } "
1011+
"C & operator= ( const C & other ) ; "
1012+
"int * p ; "
1013+
"} ; "
1014+
"} "
1015+
"N :: C c ;";
1016+
ASSERT_EQUALS(expected, tok(code));
1017+
ASSERT_EQUALS("", errout_str());
1018+
}
1019+
9941020
void simplifyUsing8970() {
9951021
const char code[] = "using V = std::vector<int>;\n"
9961022
"struct A {\n"

0 commit comments

Comments
 (0)