From 25663cd232ab0a4fdb6915f932679a54a69a2fdb Mon Sep 17 00:00:00 2001 From: HC Hagen Date: Mon, 20 Mar 2017 15:02:07 +0100 Subject: [PATCH 1/2] Return error rather than panicking on malformed json where there are more closing brackets than opening brackets --- parser.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/parser.go b/parser.go index cee8f91..fc1a80a 100644 --- a/parser.go +++ b/parser.go @@ -289,6 +289,10 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str } if maxPath >= level { + if level < 1 { + cb(-1, []byte{}, Unknown, MalformedJsonError) + return -1 + } pathsBuf[level-1] = bytesToString(&keyUnesc) for pi, p := range paths { From f86e08532fdb0489262bb72a8e78126d20734630 Mon Sep 17 00:00:00 2001 From: HC Hagen Date: Fri, 2 Jun 2017 16:31:19 +0200 Subject: [PATCH 2/2] Return error rather than panicking on malformed json where there are more closing brackets than opening brackets --- parser.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/parser.go b/parser.go index fc1a80a..4696ded 100644 --- a/parser.go +++ b/parser.go @@ -343,6 +343,12 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str case '[': var arrIdxFlags int64 var pIdxFlags int64 + + if level < 0 { + cb(-1, []byte{}, Unknown, MalformedJsonError) + return -1 + } + for pi, p := range paths { if len(p) < level+1 || pathFlags&bitwiseFlags[pi+1] != 0 || p[level][0] != '[' || !sameTree(p, pathsBuf[:level]) { continue