fix: don't panic on empty key when path enters an array - #284
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>
|
Closing as already-fixed — thank you for the report and the fix, @c-tonneslan. The same bug class (unguarded Your two regression fixtures ( The fix ships in #283 with DEFECT-260726-QS2V / KI-1. Appreciate the contribution — if you spot more parser hazards, the |
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.