[clang] [clang]Fix name lookup crash (PR #190484)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 25 03:50:27 PDT 2026
https://github.com/Serosh-commits updated https://github.com/llvm/llvm-project/pull/190484
>From 11a2cb4933c1c37684e96a72fbb716020587ab9d Mon Sep 17 00:00:00 2001
From: Serosh-commits <janmejayapanda400 at gmail.com>
Date: Sat, 25 Apr 2026 08:02:35 +0530
Subject: [PATCH] [Clang]Fix name lookup crash
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaDecl.cpp | 11 ++++++-----
clang/test/SemaCXX/gh189344.cpp | 12 ++++++++++++
3 files changed, 20 insertions(+), 5 deletions(-)
create mode 100644 clang/test/SemaCXX/gh189344.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f8508ebefb199..752e37765a484 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to Attribute Support
Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Fixed a name lookup crash when parsing invalid template parameters. (#GH189344)
- Fixed a crash when a function template is defined as a non-template friend with a global scope qualifier. (#GH185341)
- Clang now rejects constant template parameters with block pointer types, since these are not implemented anyway and would lead to crashes. (#GH189247)
- Fixed a crash on error recovery when dealing with invalid templates. (#GH183075)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 36538e18f297c..a2f70cf6ffbaa 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1488,19 +1488,20 @@ void Sema::EnterTemplatedContext(Scope *S, DeclContext *DC) {
// classes) then T then N then ::.
unsigned ScopeDepth = getTemplateDepth(S);
for (; S && S->isTemplateParamScope(); S = S->getParent(), --ScopeDepth) {
- DeclContext *SearchDCAfterScope = DC;
- for (; DC; DC = DC->getLookupParent()) {
+ DeclContext *SearchDCAfterScope = nullptr;
+ for (DeclContext *NextDC = DC; NextDC; NextDC = NextDC->getLookupParent()) {
if (const TemplateParameterList *TPL =
- cast<Decl>(DC)->getDescribedTemplateParams()) {
+ cast<Decl>(NextDC)->getDescribedTemplateParams()) {
unsigned DCDepth = TPL->getDepth() + 1;
if (DCDepth > ScopeDepth)
continue;
if (ScopeDepth == DCDepth)
- SearchDCAfterScope = DC = DC->getLookupParent();
+ SearchDCAfterScope = NextDC->getLookupParent();
break;
}
}
- S->setLookupEntity(SearchDCAfterScope);
+ if (SearchDCAfterScope)
+ S->setLookupEntity(SearchDCAfterScope);
}
}
diff --git a/clang/test/SemaCXX/gh189344.cpp b/clang/test/SemaCXX/gh189344.cpp
new file mode 100644
index 0000000000000..f656c1173c2ac
--- /dev/null
+++ b/clang/test/SemaCXX/gh189344.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template <union { } alignas ( union a { c ) ( a :: ) ( :: (b
+// expected-error at -1 {{cannot be defined in a type specifier}}
+// expected-error at -2 {{'a' cannot be defined in a type specifier}}
+// expected-error at -3 {{unknown type name 'b'}}
+// expected-error at -4 2{{parameter declarator cannot be qualified}}
+// expected-error at -5 {{a type specifier is required for all declarations}}
+// expected-note at -6 2{{to match this '('}}
+// expected-error@* 3{{expected unqualified-id}}
+// expected-error@* 2{{expected ')'}}
+// expected-error@* {{expected ',' or '>' in template-parameter-list}}
More information about the cfe-commits
mailing list