[clang] [clang] Fix friend declaration check (PR #132320)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 20 19:55:40 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Chongzhi Deng (BruceAko)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/132320.diff
1 Files Affected:
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+2-2)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/132320
More information about the cfe-commits
mailing list