Properly resize arrays when adding (#5872)

* Properly resize arrays when adding

The current logic attempts to update the array but, because
append may need to allocate a new array with adequate space,
the code can currently leave dest referring to the old,
under-sized array. The solution is to use the set(dest)
logic that already exists and is used for the IsNull case.

Added a test case that would fail before this fix and now passes.

This fixes pulumi/pulumi#5871.

* Add CHANGELOG entry
This commit is contained in:
Joe Duffy 2020-12-06 13:23:50 -08:00 committed by GitHub
parent a70cb41731
commit a5c30f1547
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 5 deletions

View file

@ -2,7 +2,9 @@ CHANGELOG
=========
## HEAD (Unreleased)
_(none)_
- Fix a problem where `pulumi import` could panic on importing arrays and sets, due to
incorrect array resizing logic. [#5872](https://github.com/pulumi/pulumi/pull/5872).
## 2.15.1 (2020-12-04)

View file

@ -187,10 +187,9 @@ func (p PropertyPath) Add(dest, v PropertyValue) (PropertyValue, bool) {
case dest.IsArray():
// If the destination array does exist, ensure that it is large enough to accommodate the requested
// index.
arr := dest.ArrayValue()
if key >= len(arr) {
arr = append(make([]PropertyValue, key+1-len(arr)), arr...)
v.V = arr
if arr := dest.ArrayValue(); key >= len(arr) {
dest = NewArrayProperty(append(make([]PropertyValue, key+1-len(arr)), arr...))
set(dest)
}
default:
return PropertyValue{}, false

View file

@ -170,3 +170,12 @@ func TestPropertyPath(t *testing.T) {
})
}
}
func TestAddResizePropertyPath(t *testing.T) {
// Regression test for https://github.com/pulumi/pulumi/issues/5871:
// Ensure that adding a new element beyond the size of an array will resize it.
path, err := ParsePropertyPath("[1]")
assert.Nil(t, err)
_, ok := path.Add(NewArrayProperty([]PropertyValue{}), NewNumberProperty(42))
assert.True(t, ok)
}