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

Crash When Intercepting URLSessionUploadTask #132

Closed
VaslD opened this issue Jul 20, 2023 · 2 comments · Fixed by #133
Closed

Crash When Intercepting URLSessionUploadTask #132

VaslD opened this issue Jul 20, 2023 · 2 comments · Fixed by #133

Comments

@VaslD
Copy link
Contributor

VaslD commented Jul 20, 2023

We recently encountered an Atlantis-related crash at:

if let task = task as? URLSessionTask,
let request = request as? NSURLRequest,
let data = data as? Data {
self?.delegate?.injectorSessionDidUpload(task: task, request: request, data: data)
} else {

Screenshot 07-20 15 17 02@2x

I've found the cause but since I'm too familiar with method swizzling and calling conventions, I think you might want to take a look and verify this.

The problem is that uploadTaskWithRequest:fromData:completionHandler:, and its Swift counterpart uploadTask(with:from:completionHandler:), has an optional Data? parameter. Atlantis swizzled this method with a C function @convention(c) (AnyObject, Selector, AnyObject, AnyObject, AnyObject) -> AnyObject that marks all parameters non-null.

A third-party library actually passes nil to the Data? parameter, which causes Atlantis to crash when reading data. Although the code uses if let, data is always implicitly unwrapped to non-optional first because the C function signature declares AnyObject (instead of AnyObject?).

Screenshot 07-20 15 35 06@2x

The fix seems to work when declaring parameters for data and completionHandler as optionals, and taking non-null data out of assertions. As a precaution, you should review other swizzled URLSessionTask factory methods to avoid implicit unwraps.

@NghiaTranUIT
Copy link
Member

@VaslD can you verify what iOS version you're using?

@VaslD
Copy link
Contributor Author

VaslD commented Jul 20, 2023

I have confirmed crash reports from iOS 15.7.2 and 15.1, with IPAs generated from Xcode 14.2 and 14.3.1. Suspicious crashes do happen on other iOS versions during QA but I have not received crash reports to corroborate those claims.

But seeing the latest API documentation (I referenced in the original post) does declare the data parameter as nullable, it would be safe to not assume that it isn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants