From 225f8d7732aba378378e7507655157b1d26cd514 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Tue, 16 Aug 2022 16:19:45 -0400 Subject: [PATCH] Updating index handling Signed-off-by: Matt Farina --- pkg/strvals/parser.go | 7 +++++++ pkg/strvals/parser_test.go | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/strvals/parser.go b/pkg/strvals/parser.go index 457b99f94..d8c73d03c 100644 --- a/pkg/strvals/parser.go +++ b/pkg/strvals/parser.go @@ -29,6 +29,10 @@ import ( // ErrNotList indicates that a non-list was treated as a list. var ErrNotList = errors.New("not a list") +// MaxIndex is the maximum index that will be allowed by setIndex. +// The default value 65536 = 1024 * 64 +var MaxIndex = 65536 + // ToYAML takes a string of arguments and converts to a YAML document. func ToYAML(s string) (string, error) { m, err := Parse(s) @@ -249,6 +253,9 @@ func setIndex(list []interface{}, index int, val interface{}) (l2 []interface{}, if index < 0 { return list, fmt.Errorf("negative %d index not allowed", index) } + if index > MaxIndex { + return list, fmt.Errorf("index of %d is greater than maximum supported index of %d", index, MaxIndex) + } if len(list) <= index { newlist := make([]interface{}, index+1) copy(newlist, list) diff --git a/pkg/strvals/parser_test.go b/pkg/strvals/parser_test.go index cef98ba0a..a1d584dc7 100644 --- a/pkg/strvals/parser_test.go +++ b/pkg/strvals/parser_test.go @@ -62,6 +62,14 @@ func TestSetIndex(t *testing.T) { val: 4, err: true, }, + { + name: "large", + initial: []interface{}{0, 1, 2, 3, 4, 5}, + expect: []interface{}{0, 1, 2, 3, 4, 5}, + add: MaxIndex + 1, + val: 4, + err: true, + }, } for _, tt := range tests {