Fix bug when copying slice to itself

If proptools.CopyProperties is passed two values that point same
slice then setting the destination slice to a new slice will
overwrite the source slice, and the properties struct that is both
the source and destination will have an empty slice.  Copy into
the new slice using a new reflect.Value, and then update the
destination.

Change-Id: I1bfcdc51e4278ea7c7ed81dafc928a5471219f05
This commit is contained in:
Colin Cross 2015-03-11 14:36:03 -07:00
parent 969c70342a
commit 62e681a288

View file

@ -64,10 +64,12 @@ func CopyProperties(dstValue, srcValue reflect.Value) {
panic(fmt.Errorf("can't copy field %q: slice elements are "+ panic(fmt.Errorf("can't copy field %q: slice elements are "+
"not strings", field.Name)) "not strings", field.Name))
} }
if srcFieldValue != dstFieldValue {
newSlice := reflect.MakeSlice(field.Type, srcFieldValue.Len(), newSlice := reflect.MakeSlice(field.Type, srcFieldValue.Len(),
srcFieldValue.Len()) srcFieldValue.Len())
reflect.Copy(newSlice, srcFieldValue)
dstFieldValue.Set(newSlice) dstFieldValue.Set(newSlice)
reflect.Copy(dstFieldValue, srcFieldValue) }
} else { } else {
dstFieldValue.Set(srcFieldValue) dstFieldValue.Set(srcFieldValue)
} }