Fix RangeError in _matchByNavigatorKeyForGoRoute during state restoration#11815
Fix RangeError in _matchByNavigatorKeyForGoRoute during state restoration#11815trippusultan wants to merge 2 commits into
Conversation
…tion When newMatchedLocation is empty (not '/'), the offset computation 'newMatchedLocation.length + 1' produces 1, which exceeds uri.path.length when the URI path is also empty. This causes ''.substring(1) to throw an unhandled RangeError in release builds where the preceding assert() guards are stripped. Clamp the offset to prevent the RangeError. Fixes flutter/flutter#185948
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
There was a problem hiding this comment.
Code Review
This pull request modifies packages/go_router/lib/src/match.dart to prevent potential out-of-bounds errors when extracting the remaining child path. It introduces a check to ensure the calculated offset does not exceed the length of uri.path, returning an empty string if it does. There are no review comments, and no further feedback is provided.
Two tests verifying that RouteMatchBase.match() does not throw RangeError when: 1. ShellRoute with empty child path (URI path doesn't start with route) 2. Nested route with path shorter than matchedLocation Fixes flutter/flutter#185948
|
Added regression tests in the latest push. Two tests cover the edge cases that trigger the RangeError:
Both tests verify that RouteMatchBase.match() returns normally without throwing RangeError. |
|
Thanks for the contribution! In the future, please do not delete the checklist that is in the PR template; it is there for a reason. This PR is missing required elements described in the checklist, which need to be addressed before it moves forward with review. I am marking the PR as a Draft. Please re-add and complete the checklist, updating the PR as appropriate, and when that’s complete please feel free to mark the PR as ready for review. |
Description
Fixes a crash in go_router's route matching logic that occurs during Android state restoration after the OS kills the Flutter process.
When
newMatchedLocationis an empty string (not'/'), the offset computationnewMatchedLocation.length + 1produces1, which exceedsuri.path.lengthwhen the URI path is also empty (which happens during restoration). This causes''.substring(1)to throw an unhandledRangeErrorin release builds where the precedingassert()guards are stripped.Changes
uri.path.substring(offset)with a clamped version:offset >= uri.path.length ? '' : uri.path.substring(offset)Related
Fixes flutter/flutter#185948
Testing