Fix index-out-of-range crash when searching the country picker - #4
Closed
jinchen1036 wants to merge 1 commit into
Closed
Fix index-out-of-range crash when searching the country picker#4jinchen1036 wants to merge 1 commit into
jinchen1036 wants to merge 1 commit into
Conversation
country(for:) subscripted filteredCountries and countries directly. isFiltering is
derived from the search bar, so it flips on the keystroke, while filteredCountries
is only replaced when the throttled search work item lands 0.25s later. A table
view can therefore ask for a row index it read before the list changed, and the
subscript traps.
Reproduced on device in a release build:
Fatal error: Index out of range
CountryCodePickerViewController.country(for:)
CountryCodePickerViewController.tableView(_:cellForRowAt:)
-[_UIFilteredDataSource tableView:cellForRowAtIndexPath:]
-[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
country(for:) now returns an optional and both call sites guard. cellForRowAt
returns the dequeued cell unconfigured — the table view asks again after the
pending reload — and didSelectRowAt returns without notifying the delegate, so a
tap on a row that no longer exists cannot deliver the wrong country.
didSelectRowAt also now deselects before the guard, so the row does not stay
highlighted when the lookup misses.
country(for:) is internal and has no callers outside this file, so the signature
change is not source-breaking for clients.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
isFilteringis derived from the search bar (searchController.isActive && !isSearchBarEmpty), so it becomes true on the keystroke — butfilteredCountriesis only replaced when the throttled work item inupdateSearchResults(for:)lands 0.25s later. A table view can therefore ask for a row index it read before the list changed, and the subscript traps.Typing narrows ~250 rows across many sections down to a handful in one, so the gap between the row count UIKit last read and the array that answers is large.