Merge pull request #56055 from sttts/sttts-object-fuzzer

Automatic merge from submit-queue (batch tested with PRs 55938, 56055, 53385, 55796, 55922). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

apiextensions: fix object keys in fuzzer to exclude escape characters

Jsoniter in ConfigFastest mode does not support escape characters in object keys. Hence, we have to fix this after the fuzzer chose invalid keys.

This might be only an intermediate fix if we decide to accept arbitrary object keys again. But for now, with the choice of `ConfigFastest` (f1258b01aa/feature_config.go (L66)) this change is necessary.
This commit is contained in:
Kubernetes Submit Queue 2017-11-21 07:43:43 -08:00 committed by GitHub
commit 21ca6bf69d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,8 +58,24 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
case reflect.Interface, reflect.Map, reflect.Slice, reflect.Ptr:
isValue = false
}
if isValue || c.Intn(5) == 0 {
if isValue || c.Intn(10) == 0 {
c.Fuzz(vobj.Field(i).Addr().Interface())
// JSON keys must not contain escape char with our JSON codec (jsoniter)
// TODO: remove this when/if we moved from jsoniter.ConfigFastest to ConfigCompatibleWithStandardLibrary
if field.Type.Kind() == reflect.Map {
keys := append([]reflect.Value(nil), vobj.Field(i).MapKeys()...)
for _, k := range keys {
stripped := toJSONString(k.String())
if stripped == k.String() {
continue
}
// set new key
vobj.Field(i).SetMapIndex(reflect.ValueOf(stripped), vobj.Field(i).MapIndex(k))
// remove old
vobj.Field(i).SetMapIndex(k, reflect.Value{})
}
}
}
}
}
@ -109,3 +125,13 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
},
}
}
func toJSONString(s string) string {
return strings.Map(func(r rune) rune {
// replace chars which are not supported in keys by jsoniter.ConfigFastest
if r == '\\' || r == '"' {
return 'x'
}
return r
}, s)
}