PackAny(): disallow types recursively more strictly

This commit is contained in:
Alexander A. Klimov 2021-05-07 12:38:18 +02:00
parent a66cfca9c8
commit 23127e3245
2 changed files with 24 additions and 6 deletions

View file

@ -71,6 +71,11 @@ func packValue(in reflect.Value, out io.Writer) error {
}
}
if l < 1 {
// Disallow (panic) some types in array/slice values (recursively), too - even if none present
_ = packValue(reflect.Zero(in.Type().Elem()), ioutil.Discard)
}
return nil
case reflect.Interface:
return packValue(in.Elem(), out)
@ -117,10 +122,23 @@ func packValue(in reflect.Value, out io.Writer) error {
}
}
if l < 1 {
typ := in.Type()
// Disallow (panic) some types in map keys and values (recursively), too - even if none present
_ = packValue(reflect.Zero(typ.Key()), ioutil.Discard)
_ = packValue(reflect.Zero(typ.Elem()), ioutil.Discard)
}
return nil
case reflect.Ptr:
if in.IsNil() {
return packValue(reflect.Value{}, out)
err := packValue(reflect.Value{}, out)
// Disallow (panic) some types in referenced value (recursively), too - even if none present
_ = packValue(reflect.Zero(in.Type().Elem()), ioutil.Discard)
return err
} else {
return packValue(in.Elem(), out)
}

View file

@ -78,8 +78,8 @@ func TestPackAny(t *testing.T) {
assertPackAnyPanic(t, float32(-42.5), 0)
assertPackAny(t, -42.5, []byte{3, 0xc0, 0x45, 0x40, 0, 0, 0, 0, 0})
assertPackAny(t, []struct{}(nil), []byte{5, 0, 0, 0, 0, 0, 0, 0, 0})
assertPackAny(t, []struct{}{}, []byte{5, 0, 0, 0, 0, 0, 0, 0, 0})
assertPackAnyPanic(t, []struct{}(nil), 9)
assertPackAnyPanic(t, []struct{}{}, 9)
assertPackAny(t, []interface{}{nil, true, -42.5}, []byte{
5, 0, 0, 0, 0, 0, 0, 0, 3,
@ -96,8 +96,8 @@ func TestPackAny(t *testing.T) {
assertPackAnyPanic(t, []interface{}{0 + 0i}, 9)
assertPackAny(t, map[struct{}]struct{}(nil), []byte{6, 0, 0, 0, 0, 0, 0, 0, 0})
assertPackAny(t, map[struct{}]struct{}{}, []byte{6, 0, 0, 0, 0, 0, 0, 0, 0})
assertPackAnyPanic(t, map[struct{}]struct{}(nil), 9)
assertPackAnyPanic(t, map[struct{}]struct{}{}, 9)
assertPackAny(t, map[interface{}]interface{}{true: "", "nil": -42.5}, []byte{
6, 0, 0, 0, 0, 0, 0, 0, 2,
@ -115,7 +115,7 @@ func TestPackAny(t *testing.T) {
assertPackAnyPanic(t, map[struct{}]struct{}{{}: {}}, 9)
assertPackAny(t, (*int)(nil), []byte{0})
assertPackAnyPanic(t, (*int)(nil), 0)
assertPackAny(t, new(float64), []byte{3, 0, 0, 0, 0, 0, 0, 0, 0})
assertPackAny(t, "", []byte{4, 0, 0, 0, 0, 0, 0, 0, 0})