Fix panic when deleting an array index equal to the array length - #121
Merged
Conversation
`Delete::delete` used the inclusive bound helper `for_len_incl`, which admits `index == len` and maps the RFC 6901 `-` token to `len`. That value was passed to `Vec::remove`, which requires `index < len`, causing a panic for pointers such as `/-` on any array (reachable from untrusted input, e.g. a JSON Patch `remove` op). Switch both the json and toml impls to the exclusive helper `for_len`, matching `resolve` and the documented contract that an unresolvable pointer returns `None`. Add out-of-bounds delete test cases. Fixes #120 Claude-Session: https://claude.ai/code/session_01XGvHsB4a2dxDVUQDGBScnW
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files
🚀 New features to boost your workflow:
|
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.
Summary
Fixes #120.
Delete::deletepanicked instead of returningNonewhen a pointer's final token resolved to an array index equal to the array's length. The RFC 6901-token always resolves to that index, so a pointer parsed from untrusted input (e.g. a JSON Patchremoveop with path/-) reached the panic.Root cause
Both the
jsonandtomlimpls used the inclusive bound helperfor_len_incl, which admitsindex == lenand mapsIndex::Next(-) tolen. The result was passed straight toVec::remove, which requiresindex < len.Fix
Switch to the exclusive helper
for_len, which rejectsNextand any numeric index>= len, so.ok()?cleanly yieldsNone. This matches whatresolvealready does and the documented contract that an unresolvable pointer returnsNonewith the document unmodified.Tests
Added out-of-bounds delete cases to both the
delete_jsonanddelete_tomlsuites:-on an array/0on an empty array/foo/-Full suite passes with
--all-features.