diff --git a/parser.go b/parser.go index c34d50d..14b80bc 100644 --- a/parser.go +++ b/parser.go @@ -308,7 +308,11 @@ func searchKeys(data []byte, keys ...string) int { case '[': // If we want to get array element by index if keyLevel == level && keys[level][0] == '[' { - aIdx, err := strconv.Atoi(keys[level][1 : len(keys[level])-1]) + var keyLen = len(keys[level]) + if keyLen < 3 || keys[level][0] != '[' || keys[level][keyLen-1] != ']' { + return -1 + } + aIdx, err := strconv.Atoi(keys[level][1 : keyLen-1]) if err != nil { return -1 } diff --git a/parser_test.go b/parser_test.go index b02ccb6..11b3e66 100644 --- a/parser_test.go +++ b/parser_test.go @@ -988,6 +988,18 @@ var getStringTests = []GetTest{ path: []string{"c"}, isErr: true, }, + { + desc: `empty array index`, + json: `[""]`, + path: []string{"[]"}, + isFound: false, + }, + { + desc: `malformed array index`, + json: `[""]`, + path: []string{"["}, + isFound: false, + }, } var getUnsafeStringTests = []GetTest{