Handle index out of range when reading arrays

This commit is contained in:
Mike Farah 2015-10-03 16:50:36 +10:00
parent c4af37ed68
commit d1c545cca0
2 changed files with 8 additions and 0 deletions

View File

@ -36,6 +36,10 @@ func recurse(value interface{}, head string, tail []string) interface{} {
} }
func readArray(array []interface{}, head int64, tail []string) interface{} { func readArray(array []interface{}, head int64, tail []string) interface{} {
if head > int64(len(array)) {
return nil
}
value := array[head] value := array[head]
if len(tail) > 0 { if len(tail) > 0 {
return recurse(value, tail[0], tail[1:len(tail)]) return recurse(value, tail[0], tail[1:len(tail)])

View File

@ -35,6 +35,10 @@ func TestReadMap_array(t *testing.T) {
assertResult(t, 4, readMap(parsedData, "b", []string{"d", "1"})) 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) { func TestWrite_simple(t *testing.T) {
write(parsedData, "b", []string{"c"}, "4") write(parsedData, "b", []string{"c"}, "4")