Skip to content

Conversation

@smanna12
Copy link
Contributor

@smanna12 smanna12 commented Nov 12, 2024

This patch correctes the handling of non-member functions in the isNormalAssignmentOperator function within CheckExprLifetime.cpp.

The previous implementation incorrectly assumed that FunctionDecl is always a CXXMethodDecl, leading to potential null pointer dereferencing.

Change: - Correctly handle the case where FD is not a CXXMethodDecl by using FD->getParamDecl(0)->getType().

This fix ensures that the function correctly handles non-member assignment operators, such as:

struct S {}; void operator|=(S, S) {}

This change improves the robustness of the isNormalAssignmentOperator function by correctly identifying and handling different types of function declarations.

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

llvmbot commented Nov 12, 2024

@llvm/pr-subscribers-clang

Author: None (smanna12)

Changes

This patch replaces dyn_cast<> with cast<> for CXXMethodDecl in isNormalAssignmentOperator() function, assuming FD is always a CXXMethodDecl. This change simplifies the code by removing the null check and relying on cast to assert the type.


Full diff: https://github.com/llvm/llvm-project/pull/115880.diff

1 Files Affected:

  • (modified) clang/lib/Sema/CheckExprLifetime.cpp (+2-2)
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index a1a402b4a2b530..e5ed9012fede51 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -482,8 +482,8 @@ static bool isNormalAssignmentOperator(const FunctionDecl *FD) {
     if (RetT->isLValueReferenceType()) {
       ASTContext &Ctx = FD->getASTContext();
       QualType LHST;
-      auto *MD = dyn_cast<CXXMethodDecl>(FD);
-      if (MD && MD->isCXXInstanceMember())
+      auto *MD = cast<CXXMethodDecl>(FD);
+      if (MD->isCXXInstanceMember())
         LHST = Ctx.getLValueReferenceType(MD->getFunctionObjectParameterType());
       else
         LHST = MD->getParamDecl(0)->getType();

@cor3ntin
Copy link
Contributor

Does this work with an explicit object member function?

Comment on lines 488 to 489
else
LHST = MD->getParamDecl(0)->getType();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
else
LHST = MD->getParamDecl(0)->getType();
else
LHST = FD->getParamDecl(0)->getType();

I’m pretty sure this is a typo and is actually supposed to be this. I don’t think a cast instead of dyn_cast is right here because this is also a valid assignment operator, but it isn’t a CXXMethodDecl:

struct S {};
void operator|=(S, S) {}

@cor3ntin’s question as to whether this handles explicit object parameters correctly is also something we should investigate, but that seems like a separate issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @Sirraide for reviews!

Copy link
Contributor Author

@smanna12 smanna12 Nov 21, 2024

Choose a reason for hiding this comment

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

@cor3ntin’s question as to whether this handles explicit object parameters correctly is also something we should investigate, but that seems like a separate issue.

Agreed. This is a separate issue.

@smanna12 smanna12 changed the title [Clang] Prevent Null Pointer Dereference in in sema::​isNormalAssignmentOperator() [Clang] Fix handling of non-member functions in isNormalAssignmentOperator() Nov 21, 2024
@smanna12 smanna12 merged commit 2369a58 into llvm:main Nov 22, 2024
8 checks passed
@smanna12 smanna12 deleted the my_branch branch November 22, 2024 02:16
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.

4 participants