mirror of https://github.com/k3s-io/k3s
Fix reflect panic in runtime/conversion
Fixes `panic: reflect: call of reflect.Value.Type on zero Value` when calling conversion.EnforcePtr() or runtime.Scheme.ObjectVersionAndKind() from default type switch. Signed-off-by: Vojtech Vitek (V-Teq) <vvitek@redhat.com>pull/6/head
parent
dc7e3d6601
commit
6a6f24b126
|
@ -113,7 +113,10 @@ func UpdateVersionAndKind(baseFields []string, versionField, version, kindField,
|
||||||
func EnforcePtr(obj interface{}) (reflect.Value, error) {
|
func EnforcePtr(obj interface{}) (reflect.Value, error) {
|
||||||
v := reflect.ValueOf(obj)
|
v := reflect.ValueOf(obj)
|
||||||
if v.Kind() != reflect.Ptr {
|
if v.Kind() != reflect.Ptr {
|
||||||
return reflect.Value{}, fmt.Errorf("expected pointer, but got %v", v.Type().Name())
|
if v.Kind() == reflect.Invalid {
|
||||||
|
return reflect.Value{}, fmt.Errorf("expected pointer, but got invalid kind")
|
||||||
|
}
|
||||||
|
return reflect.Value{}, fmt.Errorf("expected pointer, but got %v type", v.Type().Name())
|
||||||
}
|
}
|
||||||
return v.Elem(), nil
|
return v.Elem(), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -242,3 +242,14 @@ func TestMetaValuesUnregisteredConvert(t *testing.T) {
|
||||||
t.Errorf("Expected %v, got %v", e, a)
|
t.Errorf("Expected %v, got %v", e, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInvalidPtrValueKind(t *testing.T) {
|
||||||
|
var simple interface{}
|
||||||
|
switch obj := simple.(type) {
|
||||||
|
default:
|
||||||
|
_, err := EnforcePtr(obj)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error on invalid kind")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -123,6 +123,20 @@ func TestScheme(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInvalidObjectValueKind(t *testing.T) {
|
||||||
|
scheme := runtime.NewScheme()
|
||||||
|
scheme.AddKnownTypeWithName("", "Simple", &InternalSimple{})
|
||||||
|
|
||||||
|
embedded := &runtime.EmbeddedObject{}
|
||||||
|
switch obj := embedded.Object.(type) {
|
||||||
|
default:
|
||||||
|
_, _, err := scheme.ObjectVersionAndKind(obj)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error on invalid kind")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBadJSONRejection(t *testing.T) {
|
func TestBadJSONRejection(t *testing.T) {
|
||||||
scheme := runtime.NewScheme()
|
scheme := runtime.NewScheme()
|
||||||
badJSONMissingKind := []byte(`{ }`)
|
badJSONMissingKind := []byte(`{ }`)
|
||||||
|
|
Loading…
Reference in New Issue