Repository navigation
fix: reject array index tokens with a leading zero or a sign - #166
Merged
fredbi merged 1 commit intoNov 30, 2026
Merged
Conversation
RFC 6901 §4 defines an array index as "0" or digits without a leading zero. Array tokens were parsed with strconv.Atoi, which also accepts "01", "00", "+1" and "-0", so Get, Set and Offset resolved "/01" to element 1 and "/-0" to element 0. Offset also resolved a negative token such as "/-1" to element 0 of a non-empty array, because its element-skipping loop never ran. Add parseArrayIndex and use it wherever a numeric token indexes a slice or a JSON array: Get, Set (terminal and intermediate tokens), GetForToken, SetForToken and Offset. After a successful strconv.Atoi it rejects a leading sign and a leading zero on a multi-digit token. Non-numeric and overflowing tokens still return the strconv.Atoi error. All errors wrap ErrPointer. The terminal "-" token keeps its append meaning on Set, and object keys such as "01" are not affected. Signed-off-by: Mykola Dolgalov <mykdol@meta.com>
fredbi
approved these changes
Nov 30, 2026
Member
|
Thanks for this correctness check. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #166 +/- ##
==========================================
+ Coverage 93.74% 93.82% +0.08%
==========================================
Files 5 5
Lines 671 680 +9
==========================================
+ Hits 629 638 +9
Misses 42 42 ☔ View full report in Codecov by Harness. |
Contributor
Author
|
Thanks for the quick review and merge! |
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.
Change type
🔧 Bug fix
Short description
Reject signed and zero-padded array indexes in JSON pointers, as required by RFC 6901 §4.
Fixes
No existing issue.
Full description
strconv.Atoiaccepts tokens such as01,+1, and-0. Against["zero","one"],Get,Set, andOffsettreated these as ordinary indexes.Offsetalso treated/-1as a reference to element 0.Get,Set,GetForToken,SetForToken, andOffsetnow reject noncanonical array indexes. Object keys such as"01"and"+1"still resolve, and a terminal-still appends onSet.Array-index parsing errors still match
errors.Is(err, ErrPointer). Representable negative indexes onGetandSetnow report an invalid array index rather than an out-of-bounds index. Nonnumeric and overflowing indexes still unwrap to*strconv.NumError, but the error text changes:GetandSetno longer insert a newline beforeJSON pointer error, andOffsetno longer adds thetoken reference … is not a numberprefix. No exported API changes.The regression tests cover terminal and intermediate tokens, unchanged documents on a rejected
Set, object keys with the same spelling, and canonical indexes. The rejection test fails onmasterwith only the tests applied and passes with the fix.This PR was written with an AI agent (Claude Opus 5.5). Original human poster: @myk-dol.
Checklist