From 307278029cbf69bc35a7795594d970b7781de2b8 Mon Sep 17 00:00:00 2001 From: chenk008 Date: Tue, 27 Oct 2020 18:21:20 +0800 Subject: [PATCH] fix all Sting method not check nil in "k8s.io/apimachinery" --- .../apiserver/validation/validation_test.go | 24 +++++++++---------- .../apimachinery/pkg/api/meta/restmapper.go | 3 +++ .../apimachinery/pkg/api/resource/quantity.go | 3 +++ .../pkg/apis/meta/v1/group_version.go | 9 +++++++ .../apimachinery/pkg/util/intstr/intstr.go | 3 +++ .../pkg/util/validation/field/path.go | 3 +++ .../apimachinery/pkg/util/version/version.go | 3 +++ 7 files changed, 36 insertions(+), 12 deletions(-) diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go index 2250c32c4e8..059fa5b16a7 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go @@ -262,22 +262,22 @@ func TestValidateCustomResource(t *testing.T) { }, failingObjects: []failingObject{ {object: map[string]interface{}{"field": true}, expectErrs: []string{ - `: Invalid value: "": "field" must validate at least one schema (anyOf)`, + `: Invalid value: "": "field" must validate at least one schema (anyOf)`, `field: Invalid value: "boolean": field in body must be of type integer,string: "boolean"`, `field: Invalid value: "boolean": field in body must be of type integer: "boolean"`, }}, {object: map[string]interface{}{"field": 1.2}, expectErrs: []string{ - `: Invalid value: "": "field" must validate at least one schema (anyOf)`, + `: Invalid value: "": "field" must validate at least one schema (anyOf)`, `field: Invalid value: "number": field in body must be of type integer,string: "number"`, `field: Invalid value: "number": field in body must be of type integer: "number"`, }}, {object: map[string]interface{}{"field": map[string]interface{}{}}, expectErrs: []string{ - `: Invalid value: "": "field" must validate at least one schema (anyOf)`, + `: Invalid value: "": "field" must validate at least one schema (anyOf)`, `field: Invalid value: "object": field in body must be of type integer,string: "object"`, `field: Invalid value: "object": field in body must be of type integer: "object"`, }}, {object: map[string]interface{}{"field": []interface{}{}}, expectErrs: []string{ - `: Invalid value: "": "field" must validate at least one schema (anyOf)`, + `: Invalid value: "": "field" must validate at least one schema (anyOf)`, `field: Invalid value: "array": field in body must be of type integer,string: "array"`, `field: Invalid value: "array": field in body must be of type integer: "array"`, }}, @@ -308,26 +308,26 @@ func TestValidateCustomResource(t *testing.T) { }, failingObjects: []failingObject{ {object: map[string]interface{}{"field": true}, expectErrs: []string{ - `: Invalid value: "": "field" must validate all the schemas (allOf). None validated`, - `: Invalid value: "": "field" must validate at least one schema (anyOf)`, + `: Invalid value: "": "field" must validate all the schemas (allOf). None validated`, + `: Invalid value: "": "field" must validate at least one schema (anyOf)`, `field: Invalid value: "boolean": field in body must be of type integer,string: "boolean"`, `field: Invalid value: "boolean": field in body must be of type integer: "boolean"`, }}, {object: map[string]interface{}{"field": 1.2}, expectErrs: []string{ - `: Invalid value: "": "field" must validate all the schemas (allOf). None validated`, - `: Invalid value: "": "field" must validate at least one schema (anyOf)`, + `: Invalid value: "": "field" must validate all the schemas (allOf). None validated`, + `: Invalid value: "": "field" must validate at least one schema (anyOf)`, `field: Invalid value: "number": field in body must be of type integer,string: "number"`, `field: Invalid value: "number": field in body must be of type integer: "number"`, }}, {object: map[string]interface{}{"field": map[string]interface{}{}}, expectErrs: []string{ - `: Invalid value: "": "field" must validate all the schemas (allOf). None validated`, - `: Invalid value: "": "field" must validate at least one schema (anyOf)`, + `: Invalid value: "": "field" must validate all the schemas (allOf). None validated`, + `: Invalid value: "": "field" must validate at least one schema (anyOf)`, `field: Invalid value: "object": field in body must be of type integer,string: "object"`, `field: Invalid value: "object": field in body must be of type integer: "object"`, }}, {object: map[string]interface{}{"field": []interface{}{}}, expectErrs: []string{ - `: Invalid value: "": "field" must validate all the schemas (allOf). None validated`, - `: Invalid value: "": "field" must validate at least one schema (anyOf)`, + `: Invalid value: "": "field" must validate all the schemas (allOf). None validated`, + `: Invalid value: "": "field" must validate at least one schema (anyOf)`, `field: Invalid value: "array": field in body must be of type integer,string: "array"`, `field: Invalid value: "array": field in body must be of type integer: "array"`, }}, diff --git a/staging/src/k8s.io/apimachinery/pkg/api/meta/restmapper.go b/staging/src/k8s.io/apimachinery/pkg/api/meta/restmapper.go index 41b60d7319e..00bd86f51a7 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/meta/restmapper.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/meta/restmapper.go @@ -65,6 +65,9 @@ type DefaultRESTMapper struct { } func (m *DefaultRESTMapper) String() string { + if m == nil { + return "" + } return fmt.Sprintf("DefaultRESTMapper{kindToPluralResource=%v}", m.kindToPluralResource) } diff --git a/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go b/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go index ed8e3f46b6c..e0d9c783c36 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go @@ -598,6 +598,9 @@ const int64QuantityExpectedBytes = 18 // String is an expensive operation and caching this result significantly reduces the cost of // normal parse / marshal operations on Quantity. func (q *Quantity) String() string { + if q == nil { + return "" + } if len(q.s) == 0 { result := make([]byte, 0, int64QuantityExpectedBytes) number, suffix := q.CanonicalizeBytes(result) diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/group_version.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/group_version.go index bd4c6d9b586..54a0944af11 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/group_version.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/group_version.go @@ -34,6 +34,9 @@ type GroupResource struct { } func (gr *GroupResource) String() string { + if gr == nil { + return "" + } if len(gr.Group) == 0 { return gr.Resource } @@ -51,6 +54,9 @@ type GroupVersionResource struct { } func (gvr *GroupVersionResource) String() string { + if gvr == nil { + return "" + } return strings.Join([]string{gvr.Group, "/", gvr.Version, ", Resource=", gvr.Resource}, "") } @@ -64,6 +70,9 @@ type GroupKind struct { } func (gk *GroupKind) String() string { + if gk == nil { + return "" + } if len(gk.Group) == 0 { return gk.Kind } diff --git a/staging/src/k8s.io/apimachinery/pkg/util/intstr/intstr.go b/staging/src/k8s.io/apimachinery/pkg/util/intstr/intstr.go index dd4311c4dd2..c0e8927fe4e 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/intstr/intstr.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/intstr/intstr.go @@ -89,6 +89,9 @@ func (intstr *IntOrString) UnmarshalJSON(value []byte) error { // String returns the string value, or the Itoa of the int value. func (intstr *IntOrString) String() string { + if intstr == nil { + return "" + } if intstr.Type == String { return intstr.StrVal } diff --git a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/path.go b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/path.go index 2efc8eec76d..f9be7ac3391 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/path.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/path.go @@ -67,6 +67,9 @@ func (p *Path) Key(key string) *Path { // String produces a string representation of the Path. func (p *Path) String() string { + if p == nil { + return "" + } // make a slice to iterate elems := []*Path{} for ; p != nil; p = p.parent { diff --git a/staging/src/k8s.io/apimachinery/pkg/util/version/version.go b/staging/src/k8s.io/apimachinery/pkg/util/version/version.go index c0430910e7a..8c997ec4502 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/version/version.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/version/version.go @@ -195,6 +195,9 @@ func (v *Version) WithBuildMetadata(buildMetadata string) *Version { // ParseGeneric, this will not include the trailing uninterpreted portion of the version // number. func (v *Version) String() string { + if v == nil { + return "" + } var buffer bytes.Buffer for i, comp := range v.components {