-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[Clang] Prevent null pointer dereference in designated initializer check #97220
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
This patch adds a null check for the current method declaration before attempting to determine if it is a designated initializer. This prevents a potential null pointer dereference when `getCurMethodDecl()` returns nullptr, reported by static analyzer tool in clang::SemaObjC::BuildInstanceMessage().
@llvm/pr-subscribers-clang Author: None (smanna12) ChangesThis patch adds a null check for the current method declaration before attempting to determine if it is a designated initializer. This prevents a potential null pointer dereference when Full diff: https://github.com/llvm/llvm-project/pull/97220.diff 1 Files Affected:
diff --git a/clang/lib/Sema/SemaExprObjC.cpp b/clang/lib/Sema/SemaExprObjC.cpp
index 7ccecf055feed..05aa30e16ed8e 100644
--- a/clang/lib/Sema/SemaExprObjC.cpp
+++ b/clang/lib/Sema/SemaExprObjC.cpp
@@ -3206,9 +3206,11 @@ ExprResult SemaObjC::BuildInstanceMessage(
}
if (!isDesignatedInitChain) {
const ObjCMethodDecl *InitMethod = nullptr;
+ auto *CurMD = SemaRef.getCurMethodDecl();
+ if (!CurMD)
+ return nullptr;
bool isDesignated =
- SemaRef.getCurMethodDecl()->isDesignatedInitializerForTheInterface(
- &InitMethod);
+ CurMD->isDesignatedInitializerForTheInterface(&InitMethod);
assert(isDesignated && InitMethod);
(void)isDesignated;
Diag(SelLoc, SuperLoc.isValid() ?
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
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.
Thanks @smanna12. See comments, I think we should take a different approach for this case.
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.
Thanks @smanna12, the change looks good!
…eck (#97220) Summary: This patch adds an assertion in clang::SemaObjC::BuildInstanceMessage() to ensure getCurMethodDecl() returns a valid method declaration, addressing a static analyzer finding. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250674
This patch adds an assertion in clang::SemaObjC::BuildInstanceMessage() to ensure getCurMethodDecl() returns a valid method declaration, addressing a static analyzer finding.