[clang] [clang][ExprConstant] Bail out on invalid lambda capture inits (PR #138832)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed May 7 03:11:43 PDT 2025
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/138832
>From ba1c2ab6e90e603d3bb0545ff640ab07b524d656 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 7 May 2025 11:03:28 +0200
Subject: [PATCH] [clang][ExprConstant] Bail out on invalid lambda capture
inits
Fixes https://github.com/llvm/llvm-project/issues/138824
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/AST/ByteCode/Compiler.cpp | 5 ++---
clang/lib/AST/ExprConstant.cpp | 2 +-
clang/test/SemaCXX/constant-expression-cxx11.cpp | 9 +++++++++
4 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 55f774f5a672e..690a796fbe844 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -561,6 +561,8 @@ Bug Fixes in This Version
the invalid attribute location appropriately. (#GH137861)
- Fixed a crash when a malformed ``_Pragma`` directive appears as part of an
``#include`` directive. (#GH138094)
+- Fixed a crash during constant evaluation involving invalid lambda captures
+ (#GH138832)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index ae6574cf99159..3cc55c7052d23 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2932,10 +2932,9 @@ bool Compiler<Emitter>::VisitLambdaExpr(const LambdaExpr *E) {
// record with their initializers.
for (const Record::Field &F : R->fields()) {
const Expr *Init = *CaptureInitIt;
- ++CaptureInitIt;
-
- if (!Init)
+ if (!Init || Init->containsErrors())
continue;
+ ++CaptureInitIt;
if (std::optional<PrimType> T = classify(Init)) {
if (!this->visit(Init))
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5950f461e4b2..500d43accb082 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -11038,7 +11038,7 @@ bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
// If there is no initializer, either this is a VLA or an error has
// occurred.
- if (!CurFieldInit)
+ if (!CurFieldInit || CurFieldInit->containsErrors())
return Error(E);
LValue Subobject = This;
diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index dc8f4bf1666ee..0a135654fab18 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -2598,3 +2598,12 @@ void foo() {
constexpr S s[2] = { }; // expected-error {{constexpr variable 's' must be initialized by a constant expression}}
}
}
+
+namespace DoubleCapture {
+ int DC() {
+ int a = 1000;
+ static auto f =
+ [a, &a] { // expected-error {{'a' can appear only once in a capture list}}
+ };
+ }
+}
More information about the cfe-commits
mailing list