From d1c545cca06ac79591b6bf2abfa33827ace02dd3 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 3 Oct 2015 16:50:36 +1000 Subject: [PATCH] Handle index out of range when reading arrays --- data_navigator.go | 4 ++++ data_navigator_test.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/data_navigator.go b/data_navigator.go index 4983c36..e6867a4 100644 --- a/data_navigator.go +++ b/data_navigator.go @@ -36,6 +36,10 @@ func recurse(value interface{}, head string, tail []string) interface{} { } func readArray(array []interface{}, head int64, tail []string) interface{} { + if head > int64(len(array)) { + return nil + } + value := array[head] if len(tail) > 0 { return recurse(value, tail[0], tail[1:len(tail)]) diff --git a/data_navigator_test.go b/data_navigator_test.go index 71f34d8..21fddd9 100644 --- a/data_navigator_test.go +++ b/data_navigator_test.go @@ -35,6 +35,10 @@ func TestReadMap_array(t *testing.T) { assertResult(t, 4, readMap(parsedData, "b", []string{"d", "1"})) } +func TestReadMap_array_out_of_bounds(t *testing.T) { + assertResult(t, nil, readMap(parsedData, "b", []string{"d", "3"})) +} + func TestWrite_simple(t *testing.T) { write(parsedData, "b", []string{"c"}, "4")