Skip to content

Fix RangeError in _matchByNavigatorKeyForGoRoute during state restoration#11815

Draft
trippusultan wants to merge 2 commits into
flutter:mainfrom
trippusultan:fix-go-router-rangerestore-crash
Draft

Fix RangeError in _matchByNavigatorKeyForGoRoute during state restoration#11815
trippusultan wants to merge 2 commits into
flutter:mainfrom
trippusultan:fix-go-router-rangerestore-crash

Conversation

@trippusultan
Copy link
Copy Markdown

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 newMatchedLocation is an empty string (not '/'), the offset computation newMatchedLocation.length + 1 produces 1, which exceeds uri.path.length when the URI path is also empty (which happens during restoration). This causes ''.substring(1) to throw an unhandled RangeError in release builds where the preceding assert() guards are stripped.

Changes

  • Replaced the unguarded uri.path.substring(offset) with a clamped version: offset >= uri.path.length ? '' : uri.path.substring(offset)

Related

Fixes flutter/flutter#185948

Testing

  • Run go_router unit tests
  • Verify state restoration path no longer crashes

…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
@flutter-dashboard
Copy link
Copy Markdown

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.

@github-actions github-actions Bot added p: go_router triage-framework Should be looked at in framework triage labels Jun 1, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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
@trippusultan
Copy link
Copy Markdown
Author

Added regression tests in the latest push. Two tests cover the edge cases that trigger the RangeError:

  1. ShellRoute with empty child path (URI path doesn't start with route path)
  2. Nested route with path shorter than matchedLocation

Both tests verify that RouteMatchBase.match() returns normally without throwing RangeError.

@stuartmorgan-g
Copy link
Copy Markdown
Collaborator

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.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft June 2, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: go_router triage-framework Should be looked at in framework triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go_router] RangeError in _matchByNavigatorKeyForGoRoute substring during state restoration after process kill

2 participants