[clang] [clang] Fix null dereference on return in lambda attribute statement expr (PR #66643)

Piotr Fusik via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 18 09:10:42 PDT 2023


https://github.com/pfusik updated https://github.com/llvm/llvm-project/pull/66643

>From 88028509d7e5b5eaf38075c18abba498cb00087a Mon Sep 17 00:00:00 2001
From: Piotr Fusik <piotr at fusion-lang.org>
Date: Mon, 18 Sep 2023 18:07:44 +0200
Subject: [PATCH] [clang] Fix null dereference on return in lambda attribute
 statement expr

clang was crashing on a lambda attribute with a statement expression
that contained a `return`.
It attempted to access the lambda type which was unknown at that point.

Fixes https://github.com/llvm/llvm-project/issues/48527
---
 clang/docs/ReleaseNotes.rst      |  4 ++++
 clang/lib/Sema/SemaStmt.cpp      |  2 ++
 clang/test/SemaCXX/gh48527-2.cpp |  6 ++++++
 clang/test/SemaCXX/gh48527.cpp   | 10 ++++++++++
 4 files changed, 22 insertions(+)
 create mode 100644 clang/test/SemaCXX/gh48527-2.cpp
 create mode 100644 clang/test/SemaCXX/gh48527.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 500f9c9a0cda741..c6f258991c85a79 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -291,6 +291,10 @@ Bug Fixes to C++ Support
   pointers on win32.
   (`#62594 <https://github.com/llvm/llvm-project/issues/62594>`_)
 
+- Fix crash for a lambda attribute with a statement expression
+  that contains a `return`.
+  (`#48527 <https://github.com/llvm/llvm-project/issues/48527>`_)
+
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed an import failure of recursive friend class template.
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 7cc509542d5381d..10adfbc406dfbb5 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3577,6 +3577,8 @@ StmtResult Sema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc,
   CapturingScopeInfo *CurCap = cast<CapturingScopeInfo>(getCurFunction());
   QualType FnRetType = CurCap->ReturnType;
   LambdaScopeInfo *CurLambda = dyn_cast<LambdaScopeInfo>(CurCap);
+  if (CurLambda && CurLambda->CallOperator->getType().isNull())
+    return StmtError();
   bool HasDeducedReturnType =
       CurLambda && hasDeducedReturnType(CurLambda->CallOperator);
 
diff --git a/clang/test/SemaCXX/gh48527-2.cpp b/clang/test/SemaCXX/gh48527-2.cpp
new file mode 100644
index 000000000000000..c5d45f29252ca6b
--- /dev/null
+++ b/clang/test/SemaCXX/gh48527-2.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+int main() {
+    auto a = []()__attribute__((b(({ return 0; })))){}; // expected-warning {{unknown attribute 'b' ignored}}
+    return 0;
+}
diff --git a/clang/test/SemaCXX/gh48527.cpp b/clang/test/SemaCXX/gh48527.cpp
new file mode 100644
index 000000000000000..420c35be37f5191
--- /dev/null
+++ b/clang/test/SemaCXX/gh48527.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+int main() { // expected-note {{to match this '{'}}
+    auto a = [](void)__attribute__((b(({ // expected-note {{to match this '('}}
+    return 0;
+} // expected-error 3 {{expected ')'}} \
+  // expected-error {{expected ';' at end of declaration}}
+// expected-error at +2 {{expected ')'}}
+// expected-error at +1 {{expected body of lambda expression}}
+// expected-error {{expected '}'}}



More information about the cfe-commits mailing list