[clang] dc28f9d - [clang][ExprConstant] Bail out on invalid lambda capture inits (#138832)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 7 20:22:15 PDT 2025
Author: Timm Baeder
Date: 2025-05-08T05:22:11+02:00
New Revision: dc28f9d087324f77db81e7192648a17ebf036125
URL: https://github.com/llvm/llvm-project/commit/dc28f9d087324f77db81e7192648a17ebf036125
DIFF: https://github.com/llvm/llvm-project/commit/dc28f9d087324f77db81e7192648a17ebf036125.diff
LOG: [clang][ExprConstant] Bail out on invalid lambda capture inits (#138832)
Fixes https://github.com/llvm/llvm-project/issues/138824
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/AST/ByteCode/Compiler.cpp
clang/lib/AST/ExprConstant.cpp
clang/test/SemaCXX/constant-expression-cxx11.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4c25d6d4d515a..0d8c365609c36 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -564,6 +564,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