[clang] Fix a crash when a lambda capturing a local array is used in an invalid templated constructor (PR #187676)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 20 04:20:00 PDT 2026
https://github.com/hax0kartik updated https://github.com/llvm/llvm-project/pull/187676
>From f1c1210b1cdc63c6a18f99b6a74d619eeb77fe62 Mon Sep 17 00:00:00 2001
From: hax0kartik <agarwala.kartik at gmail.com>
Date: Fri, 20 Mar 2026 16:36:25 +0530
Subject: [PATCH] Fix a crash when a lambda capturing a local array is used in
an invalid templated constructor
Fix by preventing the check for potential captures if the function in which the
lambda is defined has an invalid decl.
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaExprCXX.cpp | 9 ++++++++-
clang/test/SemaCXX/GH187183.cpp | 16 ++++++++++++++++
3 files changed, 25 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/GH187183.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 45234c316eba8..401128f06b982 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -366,6 +366,7 @@ Bug Fixes to C++ Support
- Fixed a crash when `explicit(bool)` is used with an incomplete enumeration. (#GH183887)
- Fixed a crash on ``typeid`` of incomplete local types during template instantiation. (#GH63242), (#GH176397)
- Fixed a crash when an immediate-invoked ``consteval`` lambda is used as an invalid initializer. (#GH185270)
+- Fixed a crash when a lambda expression capturing local array is used in an invalid template constructor. (#GH187183)
- Fixed an assertion failure when using a global destructor with a target with a non-default program address space. (#GH186484)
- Inherited constructors in ``dllexport`` classes are now exported for ABI-compatible cases, matching
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 5553f25546c38..666ecc403c3cd 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -7812,8 +7812,15 @@ ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC,
while (isa_and_nonnull<CapturedDecl>(DC))
DC = DC->getParent();
const bool IsInLambdaDeclContext = isLambdaCallOperator(DC);
+
+ // Do not check for potential captures if the function in which the lambda
+ // is defined does not have a valid decl. (GH187183)
+ FunctionDecl *FD = getCurFunctionDecl();
+ bool IsValidParentFunc = !FD || !FD->isInvalidDecl();
+
if (IsInLambdaDeclContext && CurrentLSI &&
- CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid())
+ CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid() &&
+ IsValidParentFunc)
CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures(FE, CurrentLSI,
*this);
return MaybeCreateExprWithCleanups(FullExpr);
diff --git a/clang/test/SemaCXX/GH187183.cpp b/clang/test/SemaCXX/GH187183.cpp
new file mode 100644
index 0000000000000..90323bccb2f51
--- /dev/null
+++ b/clang/test/SemaCXX/GH187183.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+namespace GH187183 {
+ template<int T>
+ struct S {
+ S();
+ };
+
+ using X = S<0>;
+
+ template<int T> // expected-error {{template parameter list matching the non-templated nested type 'GH187183::S<0>' should be empty ('template<>')}}
+ S<0>::S() {
+ int e[1];
+ test([&e]() { return e[0]; });
+ }
+}
More information about the cfe-commits
mailing list