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

Fix infinite loop in swizzler #585

Merged
merged 3 commits into from
Jan 27, 2025
Merged

Fix infinite loop in swizzler #585

merged 3 commits into from
Jan 27, 2025

Conversation

mmaatttt
Copy link
Contributor

I discovered that one (but perhaps not the only?) way to cause the swizzling to infinite loop is with inheritance. The test case in this PR shows it, but what could happen is:

1. swizzle Delegate1 scrollViewDidEndDragging
    a. add placeholder impl
    b. add actual impl
    c. swap placeholder and actual
2. swizzle Delegate2 scrollViewDidEndDragging
    a. add actual impl
    b. swap with "original", which is actually itself (from 1.b) 

Checking that the actual implementations that we intend to swap are different is the key here.

In my investigation and testing, I also notice we were trying to swizzle the same class multiple times. The guard checks in the Swizzler prevent anything bad from happening, but it's still unnecessary, so I added a Set to track what's been swizzled to prevent repeat attempts that could potentially cause something to break.

And for the case where a scroll view has a nil delegate, we can just assign it AppcuesScrollViewDelegate.shared with the implementation we need instead of also swizzling the AppcuesScrollViewDelegate.shared since we own its implementation. This just saves us from more swizzling in this one case, making things a bit safer overall.

This should fix #582

@mmaatttt mmaatttt requested a review from iujames January 24, 2025 17:11
Copy link
Contributor

@iujames iujames left a comment

Choose a reason for hiding this comment

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

great find and improvements!

@mmaatttt mmaatttt changed the title Fix infinit loop in swizzler Fix infinite loop in swizzler Jan 24, 2025
@mmaatttt mmaatttt merged commit c71cbf9 into main Jan 27, 2025
5 checks passed
@mmaatttt mmaatttt deleted the fix/sc-74607/swizzle-loop branch January 27, 2025 14:26
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 this pull request may close these issues.

[Bug] Infinite loop crash in UIScrollView.appcues__scrollViewWillBeginDragging
2 participants