Unverified Commit 21ca6bf6 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

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` (https://github.com/json-iterator/go/blob/f1258b01aa14930fff5c6b9b09fe9b4614359472/feature_config.go#L66) this change is necessary.
parents aca38605 96036961
...@@ -58,8 +58,24 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} { ...@@ -58,8 +58,24 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
case reflect.Interface, reflect.Map, reflect.Slice, reflect.Ptr: case reflect.Interface, reflect.Map, reflect.Slice, reflect.Ptr:
isValue = false isValue = false
} }
if isValue || c.Intn(5) == 0 { if isValue || c.Intn(10) == 0 {
c.Fuzz(vobj.Field(i).Addr().Interface()) 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{} { ...@@ -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)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment