-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang] Fix handling of non-member functions in isNormalAssignmentOperator() #115880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@llvm/pr-subscribers-clang Author: None (smanna12) ChangesThis 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:
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();
|
|
Does this work with an explicit object member function? |
clang/lib/Sema/CheckExprLifetime.cpp
Outdated
| else | ||
| LHST = MD->getParamDecl(0)->getType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
This patch correctes the handling of non-member functions in the
isNormalAssignmentOperatorfunction withinCheckExprLifetime.cpp.The previous implementation incorrectly assumed that
FunctionDeclis always aCXXMethodDecl, leading to potential null pointer dereferencing.Change: - Correctly handle the case where
FDis not aCXXMethodDeclby usingFD->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
isNormalAssignmentOperatorfunction by correctly identifying and handling different types of function declarations.