Skip to content

Commit 8d5661e

Browse files
committed
Fix Issue #81: Check return value of recursive call
1 parent 7266f2b commit 8d5661e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

parser.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ func searchKeys(data []byte, keys ...string) int {
122122
}
123123

124124
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
125-
126125
for i < ln {
127126
switch data[i] {
128127
case '"':
@@ -195,7 +194,11 @@ func searchKeys(data []byte, keys ...string) int {
195194
if valueFound == nil {
196195
return -1
197196
} else {
198-
return i + valueOffset + searchKeys(valueFound, keys[level+1:]...)
197+
subIndex := searchKeys(valueFound, keys[level+1:]...)
198+
if subIndex < 0 {
199+
return -1
200+
}
201+
return i + valueOffset + subIndex
199202
}
200203
} else {
201204
// Do not search for keys inside arrays

parser_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,19 @@ var getTests = []GetTest{
357357
path: []string{"b"},
358358
isFound: false,
359359
},
360-
360+
{ // Issue #81
361+
desc: `missing key in object in array`,
362+
json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`,
363+
path: []string{"p", "a", "[0]", "x"},
364+
isFound: false,
365+
},
366+
{ // Issue #81 counter test
367+
desc: `existing key in object in array`,
368+
json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`,
369+
path: []string{"p", "a", "[0]", "u"},
370+
isFound: true,
371+
data: "abc",
372+
},
361373
{ // This test returns not found instead of a parse error, as checking for the malformed JSON would reduce performance
362374
desc: "malformed key (followed by comma followed by colon)",
363375
json: `{"a",:1}`,
@@ -620,7 +632,6 @@ func runGetTests(t *testing.T, testKind string, tests []GetTest, runner func(Get
620632
if activeTest != "" && test.desc != activeTest {
621633
continue
622634
}
623-
624635
fmt.Println("Running:", test.desc)
625636

626637
value, dataType, err := runner(test)

0 commit comments

Comments
 (0)