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

URLSessionDelegateProxy self recursion crash #171

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
"version" : "1.13.0"
}
},
{
"identity" : "opentracing-objc",
"kind" : "remoteSourceControl",
"location" : "https://github.com/undefinedlabs/opentracing-objc",
"state" : {
"revision" : "18c1a35ca966236cee0c5a714a51a73ff33384c1",
"version" : "0.5.2"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -143,6 +152,15 @@
"revision" : "6a9e38e7bd22a3b8ba80bddf395623cf68f57807",
"version" : "1.3.1"
}
},
{
"identity" : "thrift-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/undefinedlabs/Thrift-Swift",
"state" : {
"revision" : "18ff09e6b30e589ed38f90a1af23e193b8ecef8e",
"version" : "1.1.2"
}
}
],
"version" : 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ class URLSessionDelegateProxy: NSObject {
toSelector selector: Selector,
session: URLSession
) -> DelegateRespondsResult<T> {

// check if the originalDelegate responds to the selector
// First check if originalDelegate is not self to prevent recursion
if let originalDelegate = originalDelegate,
originalDelegate.responds(to: selector) {
!(originalDelegate is URLSessionDelegateProxy),
originalDelegate.responds(to: selector) {
if let delegateAsT = originalDelegate as? T {
return .respondsAndConforms(as: delegateAsT)
} else if let object = originalDelegate as? NSObject {
return .respondsWithoutConformance(object: object)
}
}

// guard that we are not the session.delegate to prevent infinite recursion
guard (session.delegate as? URLSessionDelegateProxy) != self else {
// Guard against self-referential delegation
guard let sessionDelegate = session.delegate,
!(sessionDelegate is URLSessionDelegateProxy),
sessionDelegate.responds(to: selector) else {
return .doesNotRespond
}

Expand All @@ -88,13 +90,10 @@ class URLSessionDelegateProxy: NSObject {
}

// if session delegate also responds to selector, we must call it
if let sessionDelegate = session.delegate,
sessionDelegate.responds(to: selector) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wouldn't remove this line

if let sessionDelegateAsT = sessionDelegate as? T {
return .respondsAndConforms(as: sessionDelegateAsT)
} else if let object = sessionDelegate as? NSObject {
return .respondsWithoutConformance(object: object)
}
if let sessionDelegateAsT = sessionDelegate as? T {
return .respondsAndConforms(as: sessionDelegateAsT)
} else if let object = sessionDelegate as? NSObject {
return .respondsWithoutConformance(object: object)
}

// If no case applies
Expand Down
Loading