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

Add condition to Selector call #694

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sng1996
Copy link

@sng1996 sng1996 commented Mar 18, 2024

When I created the URLSessionDelegate implementation, app crashed because I didn't implement some optional protocol methods and slf doesn't respond to those method selectors.

For example, for protocol URLSessionDataDelegate I didn't implement function
optional func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)

Comment on lines -592 to +596
((void(*)(id, SEL))objc_msgSend)(
slf, swizzledSelector
);
if ([slf respondsToSelector:swizzledSelector]) {
((void(*)(id, SEL))objc_msgSend)(
slf, swizzledSelector
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this check is necessary, since NSURLConnection will always implement -cancel. Do you agree?

Comment on lines -683 to +689

((void(*)(id, SEL))objc_msgSend)(
slf, swizzledSelector
);
if ([slf respondsToSelector:swizzledSelector]) {
((void(*)(id, SEL))objc_msgSend)(
slf, swizzledSelector
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

Comment on lines -748 to +761
((void(*)(id, SEL, id, id, id))objc_msgSend)(
slf, swizzledSelector, request, queue, wrapper
);
if ([slf respondsToSelector:swizzledSelector]) {
((void(*)(id, SEL, id, id, id))objc_msgSend)(
slf, swizzledSelector, request, queue, wrapper
);
}
} else {
((void(*)(id, SEL, id, id, id))objc_msgSend)(
slf, swizzledSelector, request, queue, completion
);
if ([slf respondsToSelector:swizzledSelector]) {
((void(*)(id, SEL, id, id, id))objc_msgSend)(
slf, swizzledSelector, request, queue, completion
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

These as well, if I am reading correctly and these are on NSURLConnection

Copy link
Collaborator

@NSExceptional NSExceptional left a comment

Choose a reason for hiding this comment

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

I stopped reviewing the PR when I realized just how many of these changes you made.

Can you add the check in the one place you need it instead of everywhere? There is an overhead to using respondsToSelector:, so we should only use it when we know the method might be optional, not in cases where we are swizzling methods that are guaranteed to exist.

@NSExceptional NSExceptional added awaiting changes … from the maintainer before merging awaiting response … from the author, to proceed bug fix labels Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes … from the maintainer before merging awaiting response … from the author, to proceed bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants