Merge pull request from GHSA-7hfp-qfw3-5jxh

Updating index handling
pull/11263/head
Matt Farina 2 years ago committed by GitHub
commit 10466e3e17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -29,6 +29,10 @@ import (
// ErrNotList indicates that a non-list was treated as a list. // ErrNotList indicates that a non-list was treated as a list.
var ErrNotList = errors.New("not 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. // ToYAML takes a string of arguments and converts to a YAML document.
func ToYAML(s string) (string, error) { func ToYAML(s string) (string, error) {
m, err := Parse(s) m, err := Parse(s)
@ -249,6 +253,9 @@ func setIndex(list []interface{}, index int, val interface{}) (l2 []interface{},
if index < 0 { if index < 0 {
return list, fmt.Errorf("negative %d index not allowed", index) 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 { if len(list) <= index {
newlist := make([]interface{}, index+1) newlist := make([]interface{}, index+1)
copy(newlist, list) copy(newlist, list)

@ -62,6 +62,14 @@ func TestSetIndex(t *testing.T) {
val: 4, val: 4,
err: true, 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 { for _, tt := range tests {

Loading…
Cancel
Save