Skip to content

LoadStateAsync accepts a saved state containing null items, and the next SaveStateAsync throws NullReferenceException #60

Closed

Description

@matt-edmondson

What's wrong

NavigationState<T>'s constructor (Navigation/Models/NavigationState.cs:22-37) validates currentIndex but not the items themselves. A persisted file such as:

{"items":[null],"currentIndex":0}

deserializes successfully. JsonFilePersistenceProvider.LoadStateAsync returns it, and NavigationStack.LoadStateAsync returns true, leaving Count == 1 and Current == null.

The failure shows up later. After NavigateTo(b) and SaveStateAsync(), NavigationState.FromNavigationStack hits the null item in its FindIndex lambda:

System.NullReferenceException
   at NavigationState`1.<FromNavigationStack>b__0  NavigationState.cs:76
   at NavigationStack`1.SaveStateAsync            NavigationStack.cs:136

This was reproduced with a temporary MSTest test.

Why it matters

A hand-edited or partly corrupted state file isn't rejected at load time, where the provider already turns invalid state into null and a clean false. Instead it is accepted, and every later save crashes the app with an unrelated-looking exception.

Suggested fix

In the NavigationState constructor, reject null entries:

if (itemList.Any(i => i is null))
    throw new ArgumentException("Navigation state items cannot be null.", nameof(items));

The provider already maps ArgumentException to a null result, so LoadStateAsync then returns false.

Acceptance: loading {"items":[null],"currentIndex":0} returns false and leaves the stack unchanged, with a test added.

Activity

  1. added
    bug
    on Sep 27, 2026
  2. matt-edmondson commented on Sep 27, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Next steps: Reject null items in the constructor with an ArgumentException, which the provider already turns into a false load. Add the {"items":[null],"currentIndex":0} test and check that the stack is left unchanged.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions