[clang] [Clang] Added nullptr check to getFriendDecl access (PR #121056)

via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 26 05:19:59 PST 2024


https://github.com/GrumpyPigSkin updated https://github.com/llvm/llvm-project/pull/121056

>From a6c7f0dfd1da4b17118f25023cf2f5da70ee3dab Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Tue, 24 Dec 2024 15:18:29 +0000
Subject: [PATCH 1/3] Added nullptr check to getFriendDecl access

---
 clang/lib/Sema/SemaDeclCXX.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index c5a72cf812ebc9..64b1fb28e2e184 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -8871,8 +8871,9 @@ bool Sema::CheckExplicitlyDefaultedComparison(Scope *S, FunctionDecl *FD,
       return true;
 
     if (llvm::none_of(RD->friends(), [&](const FriendDecl *F) {
-          return FD->getCanonicalDecl() ==
-                 F->getFriendDecl()->getCanonicalDecl();
+          if (NamedDecl* Ffd = F->getFriendDecl())
+            return FD->getCanonicalDecl() == Ffd->getCanonicalDecl();
+          return false;
         })) {
       Diag(FD->getLocation(), diag::err_defaulted_comparison_not_friend)
           << int(DCK) << int(0) << RD;

>From 884100d515904ebba05172a3a6a535b34d8a91dd Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Tue, 24 Dec 2024 15:24:08 +0000
Subject: [PATCH 2/3] Applied formatting

---
 clang/lib/Sema/SemaDeclCXX.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 64b1fb28e2e184..973318c7060a6a 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -8871,7 +8871,7 @@ bool Sema::CheckExplicitlyDefaultedComparison(Scope *S, FunctionDecl *FD,
       return true;
 
     if (llvm::none_of(RD->friends(), [&](const FriendDecl *F) {
-          if (NamedDecl* Ffd = F->getFriendDecl())
+          if (NamedDecl *Ffd = F->getFriendDecl())
             return FD->getCanonicalDecl() == Ffd->getCanonicalDecl();
           return false;
         })) {

>From 7846c140110ed87d9d7997c7ac792c5d5aa92972 Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Thu, 26 Dec 2024 13:19:20 +0000
Subject: [PATCH 3/3] Added test and release note

---
 clang/docs/ReleaseNotes.rst                    |  1 +
 clang/test/SemaCXX/friend-default-operator.cpp | 12 ++++++++++++
 2 files changed, 13 insertions(+)
 create mode 100644 clang/test/SemaCXX/friend-default-operator.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8b984ecaefecaf..cf0148296254a0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -885,6 +885,7 @@ Bug Fixes to C++ Support
 - Fixed recognition of ``std::initializer_list`` when it's surrounded with ``extern "C++"`` and exported
   out of a module (which is the case e.g. in MSVC's implementation of ``std`` module). (#GH118218)
 - Fixed a pack expansion issue in checking unexpanded parameter sizes. (#GH17042)
+- Fixed a crash when parsing a friend declaration and a defaulted operator.
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/SemaCXX/friend-default-operator.cpp b/clang/test/SemaCXX/friend-default-operator.cpp
new file mode 100644
index 00000000000000..cbb18f99416378
--- /dev/null
+++ b/clang/test/SemaCXX/friend-default-operator.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+// ; Ensure the following out of line friend declaration doesn't cause the compiler to crash.
+
+class A {
+  friend bool operator==(const A&, const A&);
+  friend class B;
+};
+
+bool operator==(const A&, const A&) = default;
+



More information about the cfe-commits mailing list