Skip to content

Conversation

@BruceAko
Copy link

@BruceAko BruceAko commented Mar 21, 2025

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.

Fixes #132249

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2025

@llvm/pr-subscribers-clang

Author: Chongzhi Deng (BruceAko)

Changes

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 --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;

@BruceAko BruceAko changed the title [clang] Fix friend declaration check [clang] Fix nullptr dereference when checking friend default comparison Mar 21, 2025
Copy link
Contributor

@mizvekov mizvekov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should add test and release note.

Comment on lines 9042 to 9043
NamedDecl *ND = F->getFriendDecl();
return ND && FD->getCanonicalDecl() == ND->getCanonicalDecl();
Copy link
Contributor

@mizvekov mizvekov Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NamedDecl *ND = F->getFriendDecl();
return ND && FD->getCanonicalDecl() == ND->getCanonicalDecl();
NamedDecl *ND = F->getFriendDecl();
return declaresSameEntity(F->getFriendDecl(), FD);

Fix comparison check for explicitly defaulted functions by using declaresSameEntity for friend declarations.
@BruceAko BruceAko closed this Mar 25, 2025
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BruceAko why did you close the PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang] clang frontend crash with friend class declaration and overloaded operator ==

4 participants