[clang] [Clang] Fixed a crash when __PRETTY_FUNCTION__ or __FUNCSIG__ (clang-cl) appears in the trailing return type of the lambda (PR #122611)

via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 11 11:52:55 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: TilakChad (TilakChad)

<details>
<summary>Changes</summary>

The (function) type of the lambda function is null while parsing trailing return type. The type is filled-in when the lambda body is entered. So, resolving `__PRETTY_FUNCTION__` before the lambda body is entered causes the crash. 

This resolves the crash: https://github.com/llvm/llvm-project/issues/121274. 

---
Full diff: https://github.com/llvm/llvm-project/pull/122611.diff


2 Files Affected:

- (modified) clang/lib/AST/Expr.cpp (+6-1) 
- (added) clang/test/SemaCXX/crash-GH121274.cpp (+15) 


``````````diff
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 5331357b5d1fef..0caff41c8a8cf6 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -774,7 +774,12 @@ std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
     const FunctionDecl *Decl = FD;
     if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
       Decl = Pattern;
-    const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
+
+    const Type *Ty = Decl->getType().getTypePtrOrNull();
+    if (!Ty)
+      return "";
+
+    const FunctionType *AFT = Ty->getAs<FunctionType>();
     const FunctionProtoType *FT = nullptr;
     if (FD->hasWrittenPrototype())
       FT = dyn_cast<FunctionProtoType>(AFT);
diff --git a/clang/test/SemaCXX/crash-GH121274.cpp b/clang/test/SemaCXX/crash-GH121274.cpp
new file mode 100644
index 00000000000000..28677a0949bf9e
--- /dev/null
+++ b/clang/test/SemaCXX/crash-GH121274.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -std=c++11 -verify %s
+// expected-no-diagnostics
+
+// Do not crash when __PRETTY_FUNCTION__ appears in the trailing return type of the lambda
+void foo() {
+	[]() -> decltype(static_cast<const char*>(__PRETTY_FUNCTION__)) {
+		return nullptr;
+	}();
+
+#ifdef MS
+	[]() -> decltype(static_cast<const char*>(__FUNCSIG__)) {
+		return nullptr;
+	}();
+#endif
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/122611


More information about the cfe-commits mailing list