Skip to content

Commit f0ee9eb

Browse files
committed
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
1 parent 5100d69 commit f0ee9eb

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

gjson.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ func squash(json string) string {
498498
}
499499
}
500500
if depth == 0 {
501+
if i >= len(json) {
502+
return json
503+
}
501504
return json[:i+1]
502505
}
503506
case '{', '[', '(':

gjson_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,3 +2178,21 @@ func TestJoin152(t *testing.T) {
21782178
res := Get(json, "historical.summary.days.#.hours|@flatten|#.humidity.value")
21792179
assert(t, res.Raw == `[92.0,92.0,91.0,91.0,67.0]`)
21802180
}
2181+
2182+
func TestIssue192(t *testing.T) {
2183+
2184+
assert(t, squash(`"000"hello`) == `"000"`)
2185+
assert(t, squash(`"000"`) == `"000"`)
2186+
assert(t, squash(`"000`) == `"000`)
2187+
assert(t, squash(`"`) == `"`)
2188+
2189+
assert(t, squash(`[000]hello`) == `[000]`)
2190+
assert(t, squash(`[000]`) == `[000]`)
2191+
assert(t, squash(`[000`) == `[000`)
2192+
assert(t, squash(`[`) == `[`)
2193+
assert(t, squash(`]`) == `]`)
2194+
2195+
testJSON := `0.#[[{}]].@valid:"000`
2196+
Get(testJSON, testJSON)
2197+
2198+
}

0 commit comments

Comments
 (0)