From 91ac96899e492584984ded0c8f9a08f10b473717 Mon Sep 17 00:00:00 2001 From: Allen Date: Sun, 22 Mar 2020 02:54:10 +0800 Subject: [PATCH] fix issue 188 (#192) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description**: This pr fix issue #188. If `findKeyStart` meets a `[` or `{`, it should not add i with `blockEnd`’s return value directly because it may return -1 if it did not find the close symbol --- parser.go | 10 ++++++++-- parser_test.go | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/parser.go b/parser.go index 513125f..2da0323 100644 --- a/parser.go +++ b/parser.go @@ -98,9 +98,15 @@ func findKeyStart(data []byte, key string) (int, error) { } case '[': - i = blockEnd(data[i:], data[i], ']') + i + end := blockEnd(data[i:], data[i], ']') + if end != -1 { + i = i + end + } case '{': - i = blockEnd(data[i:], data[i], '}') + i + end := blockEnd(data[i:], data[i], '}') + if end != -1 { + i = i + end + } } i++ } diff --git a/parser_test.go b/parser_test.go index 18045f1..40685cf 100644 --- a/parser_test.go +++ b/parser_test.go @@ -185,6 +185,18 @@ var deleteTests = []DeleteTest{ path: []string{"b"}, data: `{"a": "1" , "c": 3}`, }, + { + desc: "Issue #188: infinite loop in Delete", + json: `^_�^C^A^@[`, + path: []string{""}, + data: `^_�^C^A^@[`, + }, + { + desc: "Issue #188: infinite loop in Delete", + json: `^_�^C^A^@{`, + path: []string{""}, + data: `^_�^C^A^@{`, + }, } var setTests = []SetTest{