Skip to content

fix: Handle nil URL in NSData swizzling - #9535

Merged
itaybre merged 3 commits into
mainfrom
fix/nsdata-nil-url
Nov 5, 2026
Merged

itaybre merged 3 commits into
mainfrom
fix/nsdata-nil-url

Conversation

@itaybre

@itaybre itaybre commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

📜 Description

The swizzled -[NSData initWithContentsOfURL:options:error:] now calls the original implementation without tracking when the URL is nil.

Objective-C callers can pass a nil URL. MessageUI does this when sharing an attachment to Mail (-[MFAttachmentSecurityScope data] → +[NSData dataWithContentsOfURL:options:error:]). The swizzle forwarded the nil URL to SentryFileIOTracker.measureNSData(from:...). That method takes a non-optional Swift URL, so URL._unconditionallyBridgeFromObjectiveC trapped with EXC_BREAKPOINT.

The NSString path swizzles aren't affected, because Swift bridges a nil NSString to an empty String.

💡 Motivation and Context

Fixes #9519
Fixes COCOA-2144

💚 How did you test it?

  • Added testInitWithContentsOfURLOptionsError_whenURLIsNil_shouldCallOriginalWithoutTracking. It calls +dataWithContentsOfURL:options:error: with nil through its IMP.
  • Without the fix, the test process crashes. With the fix, it passes on macOS and on the iOS 18.4 simulator.
  • Foundation's own handling of a nil URL depends on the OS: the macOS 26 test host raises NSInvalidArgumentException. The test therefore checks that the swizzled call has the same outcome as the unswizzled call and doesn't reach the tracker.
  • SentryNSDataSwizzlingHelperTests, DataSentryTracingIntegrationTests, and SentryFileIOTrackerTests pass on macOS and iOS. make format and make analyze pass.

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.
  • If I added a new public API, I also added it to the SentryObjC wrapper.

Objective-C callers such as MessageUI can read NSData from a nil URL.
The swizzled initWithContentsOfURL:options:error: forwarded it to
SentryFileIOTracker, whose Swift URL parameter is non-optional, so
bridging nil trapped with EXC_BREAKPOINT.

Call the original implementation untracked when the URL is nil.

Fixes #9519
Fixes COCOA-2144
@itaybre
itaybre requested review from a team and noahsmartin as code owners November 4, 2026 18:57
@itaybre
itaybre requested review from NinjaLikesCheez and philprime and removed request for a team November 4, 2026 18:57
@linear-code

linear-code Bot commented Nov 4, 2026

Copy link
Copy Markdown

COCOA-2144

@philprime philprime left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@itaybre itaybre added the run-full-ci Allows gated GitHub Action workflows to run for a labelled pull request label Nov 5, 2026
@itaybre
itaybre enabled auto-merge (squash) November 5, 2026 14:01
@sentry

sentry Bot commented Nov 5, 2026

Copy link
Copy Markdown

Size Analysis

1 component analyzed

iOS Builds

Name Configuration Version Download Size Install Size
SDK-Size (iOS)
io.sentry.sample.SDK-Size
Release 9.33.0 (1) 1.2 MB (+20 B) 3.2 MB (0 B)

Configure sentry-cocoa status check rules

@sentry

sentry Bot commented Nov 5, 2026

Copy link
Copy Markdown

📲 Install Builds

iOS

🔗 App Name App ID Version Configuration
SDK-Size io.sentry.sample.SDK-Size 9.33.0 (1) Release

⚙️ sentry-cocoa Build Distribution Settings

@itaybre
itaybre merged commit 5d1b960 into main Nov 5, 2026
303 of 305 checks passed
@itaybre
itaybre deleted the fix/nsdata-nil-url branch November 5, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved run-full-ci Allows gated GitHub Action workflows to run for a labelled pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EXC_BREAKPOINT in NSData swizzling when initWithContentsOfURL:options:error: receives a nil URL

2 participants