Fix bounds out of range panic

This commit fixes an issues where gjson panics when the input json
or path ends in incomplete quoted string.

fixes #192
This commit is contained in:
tidwall 2020-12-07 05:25:20 -07:00
parent 5100d6926a
commit f0ee9ebde4
2 changed files with 21 additions and 0 deletions

View File

@ -498,6 +498,9 @@ func squash(json string) string {
}
}
if depth == 0 {
if i >= len(json) {
return json
}
return json[:i+1]
}
case '{', '[', '(':

View File

@ -2178,3 +2178,21 @@ func TestJoin152(t *testing.T) {
res := Get(json, "historical.summary.days.#.hours|@flatten|#.humidity.value")
assert(t, res.Raw == `[92.0,92.0,91.0,91.0,67.0]`)
}
func TestIssue192(t *testing.T) {
assert(t, squash(`"000"hello`) == `"000"`)
assert(t, squash(`"000"`) == `"000"`)
assert(t, squash(`"000`) == `"000`)
assert(t, squash(`"`) == `"`)
assert(t, squash(`[000]hello`) == `[000]`)
assert(t, squash(`[000]`) == `[000]`)
assert(t, squash(`[000`) == `[000`)
assert(t, squash(`[`) == `[`)
assert(t, squash(`]`) == `]`)
testJSON := `0.#[[{}]].@valid:"000`
Get(testJSON, testJSON)
}