Properly handle nil interfaces in DeepCopy.
Running reflect.ValueOf(X) where X is a nil interface will return a zero Value. We cannot get the type (because no concrete type is known) and cannot check if the Value is nil later on due to the way reflect.Value works. So we should handle this case by immediately returning nil. We cannot type-assert a nil interface to another interface type (as no concrete type is assigned), so we must add another check to see if the returned interface is nil.
This commit is contained in:
@@ -117,6 +117,13 @@ func (c *Cloner) RegisterGeneratedDeepCopyFunc(deepCopyFunc interface{}) error {
|
||||
|
||||
// DeepCopy will perform a deep copy of a given object.
|
||||
func (c *Cloner) DeepCopy(in interface{}) (interface{}, error) {
|
||||
// Can be invalid if we run DeepCopy(X) where X is a nil interface type.
|
||||
// For example, we get an invalid value when someone tries to deep-copy
|
||||
// a nil labels.Selector.
|
||||
// This does not occur if X is nil and is a pointer to a concrete type.
|
||||
if in == nil {
|
||||
return nil, nil
|
||||
}
|
||||
inValue := reflect.ValueOf(in)
|
||||
outValue, err := c.deepCopy(inValue)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user