Skip to content
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

[Clang] Fix potential null pointer dereferences in retain cycle detection #95192

Closed
wants to merge 3 commits into from

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Jun 12, 2024

This patch resolves a static analyzer bugs where S.getCurMethodDecl() or SemaRef.getCurMethodDecl() could return nullptr when calling getSelfDecl(() and was being dereferenced without a null check. The fix introduces a check for a non-null return value before accessing getSelfDecl() to ensure safe dereferencing.

This change prevents undefined behavior in scenarios where the current method declaration is not available, thus enhancing the robustness of the retain cycle detection logic.

This patch resolves a static analyzer bug where `S.getCurMethodDecl()` could return `nullptr` when calling getSelfDecl(() and was being dereferenced without a null check. The fix introduces a check for a non-null return value before accessing `getSelfDecl()` to ensure safe dereferencing.

This change prevents undefined behavior in scenarios where the current method declaration is not available, thus enhancing the robustness of the retain cycle detection logic.
@smanna12 smanna12 requested a review from tahonermann June 12, 2024 02:56
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 12, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 12, 2024

@llvm/pr-subscribers-clang

Author: None (smanna12)

Changes

This patch resolves a static analyzer bug where S.getCurMethodDecl() could return nullptr when calling getSelfDecl(() and was being dereferenced without a null check. The fix introduces a check for a non-null return value before accessing getSelfDecl() to ensure safe dereferencing.

This change prevents undefined behavior in scenarios where the current method declaration is not available, thus enhancing the robustness of the retain cycle detection logic.


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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaObjC.cpp (+9-5)
diff --git a/clang/lib/Sema/SemaObjC.cpp b/clang/lib/Sema/SemaObjC.cpp
index d396258cfc7d1..69c78f034bd43 100644
--- a/clang/lib/Sema/SemaObjC.cpp
+++ b/clang/lib/Sema/SemaObjC.cpp
@@ -848,12 +848,16 @@ static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
 
       owner.Indirect = true;
       if (pre->isSuperReceiver()) {
-        owner.Variable = S.getCurMethodDecl()->getSelfDecl();
-        if (!owner.Variable)
+        if (const auto *CurMethodDecl = S.getCurMethodDecl()) {
+          owner.Variable = CurMethodDecl()->getSelfDecl();
+          if (!owner.Variable)
+            return false;
+          owner.Loc = pre->getLocation();
+          owner.Range = pre->getSourceRange();
+          return true;
+        } else {
           return false;
-        owner.Loc = pre->getLocation();
-        owner.Range = pre->getSourceRange();
-        return true;
+        }
       }
       e = const_cast<Expr *>(
           cast<OpaqueValueExpr>(pre->getBase())->getSourceExpr());

@smanna12 smanna12 requested a review from Endilll June 12, 2024 02:59
@smanna12 smanna12 changed the title [Clang] Fix potential null pointer dereference in retain cycle detection [Clang] Fix potential null pointer dereferences in retain cycle detection Jun 12, 2024
@Endilll Endilll requested a review from rjmccall June 12, 2024 07:12
@rjmccall
Copy link
Contributor

Do you have a test case?

@smanna12
Copy link
Contributor Author

Do you have a test case?

Thanks @rjmccall for reviews. No I do not have any test case. The issue is reported by static analyzer tool.

@rjmccall
Copy link
Contributor

Okay. It looks like it's actually impossible for this to be null — since we're looking at a super dispatch, we must be inside an Objective-C method declaration. We do appreciate people running static analysis on our code base, but please think of analysis reports as the start of an investigation rather than something that needs to be reflexively fixed.

@rjmccall rjmccall closed this Jun 14, 2024
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.

3 participants