Skip to content

fix: don't panic on empty key when path enters an array#284

Open
c-tonneslan wants to merge 1 commit into
buger:masterfrom
c-tonneslan:fix/empty-key-array-panic
Open

fix: don't panic on empty key when path enters an array#284
c-tonneslan wants to merge 1 commit into
buger:masterfrom
c-tonneslan:fix/empty-key-array-panic

Conversation

@c-tonneslan
Copy link
Copy Markdown

Description: For a Get call like jsonparser.Get([]byte("[]"), "") the searchKeys '[' case reached keys[level][0] == '[' with an empty keys[level] and indexed into a zero-length string, panicking with index out of range [0] with length 0. The reproducer is exactly the one in #247:

jsonparser.Get([]byte(`[]`), "")
jsonparser.Get([]byte(`[`), "")

Both used to panic where the object equivalents ({} and {) correctly return KeyPathNotFoundError. After this change they fall through to the same array-skip branch and surface KeyPathNotFoundError instead.

Two regression cases added to getArrayTests.

Benchmark before change: this is a guard on an exceptional input. The hot path is untouched and the new check is a length comparison on a string already in cache, so I didn't expect (or measure) a perceivable benchmark delta. Happy to run make bench here if you'd like the numbers.

Closes #247.

…array

For a Get call like jsonparser.Get([]byte("[]"), "") the searchKeys
'[' case reached `keys[level][0] == '['` with an empty `keys[level]`
and indexed into a zero-length string, panicking with index out of
range. Guard the index access so an empty key falls through to the
array-skip branch and surfaces as KeyPathNotFoundError, matching the
behaviour for the object case (`Get([]byte("{}"), "")`).

Closes buger#247

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
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.

Get call panics on empty json array or invalid json with an empty path - expecting an error to be returned

1 participant