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:
parent
969c70342a
commit
62e681a288
1 changed files with 6 additions and 4 deletions
|
@ -64,10 +64,12 @@ func CopyProperties(dstValue, srcValue reflect.Value) {
|
|||
panic(fmt.Errorf("can't copy field %q: slice elements are "+
|
||||
"not strings", field.Name))
|
||||
}
|
||||
if srcFieldValue != dstFieldValue {
|
||||
newSlice := reflect.MakeSlice(field.Type, srcFieldValue.Len(),
|
||||
srcFieldValue.Len())
|
||||
reflect.Copy(newSlice, srcFieldValue)
|
||||
dstFieldValue.Set(newSlice)
|
||||
reflect.Copy(dstFieldValue, srcFieldValue)
|
||||
}
|
||||
} else {
|
||||
dstFieldValue.Set(srcFieldValue)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue