Skip to content

Commit 146479e

Browse files
authored
Merge pull request #52 from pendo-io/malformed-array-bugfix
Bugfix searchKeys for malformed JSON arrays
2 parents 0beeeb2 + 843f7b6 commit 146479e

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

parser.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ func searchKeys(data []byte, keys ...string) int {
169169
level--
170170
case '[':
171171
// Do not search for keys inside arrays
172-
arraySkip := blockEnd(data[i:], '[', ']')
173-
i += arraySkip - 1
172+
if arraySkip := blockEnd(data[i:], '[', ']'); arraySkip == -1 {
173+
return -1
174+
} else {
175+
i += arraySkip - 1
176+
}
174177
}
175178

176179
i++

parser_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ var getTests = []GetTest{
292292
path: []string{"a"},
293293
isErr: true,
294294
},
295+
GetTest{
296+
desc: `malformed array (no closing brace)`,
297+
json: `{"a":[, "b":123}`,
298+
path: []string{"b"},
299+
isErr: true,
300+
},
295301

296302
GetTest{ // This test returns not found instead of a parse error, as checking for the malformed JSON would reduce performance
297303
desc: "malformed key (followed by comma followed by colon)",

0 commit comments

Comments
 (0)