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

via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 7 09:46:18 PST 2025


https://github.com/cor3ntin 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/5] 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/5] 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/5] 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;
+

>From 5d31475ab3bfd8349087b625dff2d009adef8c39 Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Thu, 26 Dec 2024 13:28:29 +0000
Subject: [PATCH 4/5] removed redundant ;

---
 clang/test/SemaCXX/friend-default-operator.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/SemaCXX/friend-default-operator.cpp b/clang/test/SemaCXX/friend-default-operator.cpp
index cbb18f99416378..b92e417a1e313c 100644
--- a/clang/test/SemaCXX/friend-default-operator.cpp
+++ b/clang/test/SemaCXX/friend-default-operator.cpp
@@ -1,7 +1,7 @@
 // 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.
+// Ensure the following out of line friend declaration doesn't cause the compiler to crash.
 
 class A {
   friend bool operator==(const A&, const A&);

>From 025f7240e45fc5b365daca09bfab4ce2637d1bf8 Mon Sep 17 00:00:00 2001
From: GrumpyPigSkin <oliver61 at live.co.uk>
Date: Tue, 7 Jan 2025 17:28:43 +0000
Subject: [PATCH 5/5] Added issue number to test and release note

---
 clang/docs/ReleaseNotes.rst                    | 2 +-
 clang/test/SemaCXX/friend-default-operator.cpp | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b15cb0f7f0f99c..711286d984a1c6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -886,7 +886,7 @@ Bug Fixes to C++ Support
   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 bug where captured structured bindings were modifiable inside non-mutable lambda (#GH95081)
-- Fixed a crash when parsing a friend declaration and a defaulted operator.
+- Fixed a crash when parsing a friend declaration and a defaulted operator. (#GH120857)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/SemaCXX/friend-default-operator.cpp b/clang/test/SemaCXX/friend-default-operator.cpp
index b92e417a1e313c..2bbd22293e32fd 100644
--- a/clang/test/SemaCXX/friend-default-operator.cpp
+++ b/clang/test/SemaCXX/friend-default-operator.cpp
@@ -3,6 +3,9 @@
 
 // Ensure the following out of line friend declaration doesn't cause the compiler to crash.
 
+namespace GH120857
+{
+
 class A {
   friend bool operator==(const A&, const A&);
   friend class B;
@@ -10,3 +13,5 @@ class A {
 
 bool operator==(const A&, const A&) = default;
 
+}
+



More information about the cfe-commits mailing list