fix: don't panic on empty key when path enters an array#284
Open
c-tonneslan wants to merge 1 commit into
Open
Conversation
…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>
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.
Description: For a
Getcall likejsonparser.Get([]byte("[]"), "")thesearchKeys'['case reachedkeys[level][0] == '['with an emptykeys[level]and indexed into a zero-length string, panicking withindex out of range [0] with length 0. The reproducer is exactly the one in #247:Both used to panic where the object equivalents (
{}and{) correctly returnKeyPathNotFoundError. After this change they fall through to the same array-skip branch and surfaceKeyPathNotFoundErrorinstead.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 benchhere if you'd like the numbers.Closes #247.