[clang] [clang] Fix friend declaration check (PR #132320)
Chongzhi Deng via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 20 19:54:50 PDT 2025
https://github.com/BruceAko created https://github.com/llvm/llvm-project/pull/132320
Fix a crash in `SemaDeclCXX.cpp` when defaulting a comparison operator that has been declared as a friend function alongside class friend declarations. When checking if a defaulted operator is a friend, the code would dereference the result of `FriendDecl::getFriendDecl()` without checking for nullptr, which occurs for class friend declarations.
The fix ensures we check that `getFriendDecl()` returns a non-null pointer before attempting to access its canonical declaration.
>From 9a8ae33973d7f74dc467f5230c1bf31676c16c85 Mon Sep 17 00:00:00 2001
From: BruceAko <chongzhi at hust.edu.cn>
Date: Fri, 21 Mar 2025 02:49:01 +0000
Subject: [PATCH] [clang] Fix friend declaration check
Signed-off-by: BruceAko <chongzhi at hust.edu.cn>
---
clang/lib/Sema/SemaDeclCXX.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index a1551e8027cd3..eae2bbf65db31 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9039,8 +9039,8 @@ bool Sema::CheckExplicitlyDefaultedComparison(Scope *S, FunctionDecl *FD,
return true;
if (llvm::none_of(RD->friends(), [&](const FriendDecl *F) {
- return FD->getCanonicalDecl() ==
- F->getFriendDecl()->getCanonicalDecl();
+ NamedDecl *ND = F->getFriendDecl();
+ return ND && FD->getCanonicalDecl() == ND->getCanonicalDecl();
})) {
Diag(FD->getLocation(), diag::err_defaulted_comparison_not_friend)
<< int(DCK) << int(0) << RD;
More information about the cfe-commits
mailing list