From 8d5661efee7f40f94b350dbcd95f7bfa9b3ba749 Mon Sep 17 00:00:00 2001 From: Adam Jensen Date: Sat, 25 Mar 2017 09:24:39 -0600 Subject: [PATCH 1/2] Fix Issue #81: Check return value of recursive call --- parser.go | 7 +++++-- parser_test.go | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/parser.go b/parser.go index cee8f91..3e7ef48 100644 --- a/parser.go +++ b/parser.go @@ -122,7 +122,6 @@ func searchKeys(data []byte, keys ...string) int { } var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings - for i < ln { switch data[i] { case '"': @@ -195,7 +194,11 @@ func searchKeys(data []byte, keys ...string) int { if valueFound == nil { return -1 } else { - return i + valueOffset + searchKeys(valueFound, keys[level+1:]...) + subIndex := searchKeys(valueFound, keys[level+1:]...) + if subIndex < 0 { + return -1 + } + return i + valueOffset + subIndex } } else { // Do not search for keys inside arrays diff --git a/parser_test.go b/parser_test.go index 89be330..2846f6b 100644 --- a/parser_test.go +++ b/parser_test.go @@ -357,7 +357,19 @@ var getTests = []GetTest{ path: []string{"b"}, isFound: false, }, - + { // Issue #81 + desc: `missing key in object in array`, + json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`, + path: []string{"p", "a", "[0]", "x"}, + isFound: false, + }, + { // Issue #81 counter test + desc: `existing key in object in array`, + json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`, + path: []string{"p", "a", "[0]", "u"}, + isFound: true, + data: "abc", + }, { // This test returns not found instead of a parse error, as checking for the malformed JSON would reduce performance desc: "malformed key (followed by comma followed by colon)", json: `{"a",:1}`, @@ -620,7 +632,6 @@ func runGetTests(t *testing.T, testKind string, tests []GetTest, runner func(Get if activeTest != "" && test.desc != activeTest { continue } - fmt.Println("Running:", test.desc) value, dataType, err := runner(test) From 4b6934ce1c45319a3620ca259525cf307f885bfa Mon Sep 17 00:00:00 2001 From: Adam Jensen Date: Sat, 25 Mar 2017 09:30:31 -0600 Subject: [PATCH 2/2] Revert whitespace changes --- parser.go | 1 + parser_test.go | 1 + 2 files changed, 2 insertions(+) diff --git a/parser.go b/parser.go index 3e7ef48..ca6d4bc 100644 --- a/parser.go +++ b/parser.go @@ -122,6 +122,7 @@ func searchKeys(data []byte, keys ...string) int { } var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings + for i < ln { switch data[i] { case '"': diff --git a/parser_test.go b/parser_test.go index 2846f6b..5f5ab66 100644 --- a/parser_test.go +++ b/parser_test.go @@ -632,6 +632,7 @@ func runGetTests(t *testing.T, testKind string, tests []GetTest, runner func(Get if activeTest != "" && test.desc != activeTest { continue } + fmt.Println("Running:", test.desc) value, dataType, err := runner(test)