Skip to content

Fix panic when deleting an array index equal to the array length - #121

Merged
asmello merged 1 commit into
mainfrom
fix/delete-array-index-oob-120
Jul 26, 2026
Merged

Fix panic when deleting an array index equal to the array length#121
asmello merged 1 commit into
mainfrom
fix/delete-array-index-oob-120

Conversation

@asmello

@asmello asmello commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #120.

Delete::delete panicked instead of returning None when 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 Patch remove op with path /-) reached the panic.

removal index (is 3) should be < len (is 3)

Root cause

Both the json and toml impls used the inclusive bound helper for_len_incl, which admits index == len and maps Index::Next (-) to len. The result was passed straight to Vec::remove, which requires index < len.

Fix

Switch to the exclusive helper for_len, which rejects Next and any numeric index >= len, so .ok()? cleanly yields None. This matches what resolve already does and the documented contract that an unresolvable pointer returns None with the document unmodified.

Tests

Added out-of-bounds delete cases to both the delete_json and delete_toml suites:

  • - on an array
  • a numeric index equal to the array length
  • /0 on an empty array
  • a nested /foo/-

Full suite passes with --all-features.

`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-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.6%. Comparing base (5265901) to head (56c5849).

Additional details and impacted files
Files with missing lines Coverage Δ
src/delete.rs 99.4% <100.0%> (+0.1%) ⬆️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@asmello
asmello merged commit 8b4dfd3 into main Jul 26, 2026
18 of 19 checks passed
@asmello
asmello deleted the fix/delete-array-index-oob-120 branch July 26, 2026 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Delete on a JSON array panics when the index equals the array length

2 participants