Skip to content

Commit d1c545c

Browse files
committed
Handle index out of range when reading arrays
1 parent c4af37e commit d1c545c

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

data_navigator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func recurse(value interface{}, head string, tail []string) interface{} {
3636
}
3737

3838
func readArray(array []interface{}, head int64, tail []string) interface{} {
39+
if head > int64(len(array)) {
40+
return nil
41+
}
42+
3943
value := array[head]
4044
if len(tail) > 0 {
4145
return recurse(value, tail[0], tail[1:len(tail)])

data_navigator_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func TestReadMap_array(t *testing.T) {
3535
assertResult(t, 4, readMap(parsedData, "b", []string{"d", "1"}))
3636
}
3737

38+
func TestReadMap_array_out_of_bounds(t *testing.T) {
39+
assertResult(t, nil, readMap(parsedData, "b", []string{"d", "3"}))
40+
}
41+
3842
func TestWrite_simple(t *testing.T) {
3943

4044
write(parsedData, "b", []string{"c"}, "4")

0 commit comments

Comments
 (0)