Fix decoding of map type that contains indirect element type

This commit is contained in:
Masaaki Goshima 2021-08-31 12:21:08 +09:00
parent 284c108638
commit d494b03b74
2 changed files with 15 additions and 4 deletions

View File

@ -3767,6 +3767,7 @@ func TestIssue282(t *testing.T) {
}
type T0 map[string]T2
// T2 size is 136 bytes. This is indirect type.
var v T0
if err := json.Unmarshal(J, &v); err != nil {
t.Fatal(err)

View File

@ -20,14 +20,11 @@ type mapDecoder struct {
}
func newMapDecoder(mapType *runtime.Type, keyType *runtime.Type, keyDec Decoder, valueType *runtime.Type, valueDec Decoder, structName, fieldName string) *mapDecoder {
isStructIndirect := valueType.Kind() == reflect.Struct && runtime.IfaceIndir(valueType)
stringKeyType := keyType.Kind() == reflect.String
canUseAssignFaststrType := stringKeyType && !isStructIndirect
return &mapDecoder{
mapType: mapType,
keyDecoder: keyDec,
keyType: keyType,
canUseAssignFaststrType: canUseAssignFaststrType,
canUseAssignFaststrType: canUseAssignFaststrType(keyType, valueType),
valueType: valueType,
valueDecoder: valueDec,
structName: structName,
@ -35,6 +32,19 @@ func newMapDecoder(mapType *runtime.Type, keyType *runtime.Type, keyDec Decoder,
}
}
const (
mapMaxElemSize = 128
)
// See detail: https://github.com/goccy/go-json/pull/283
func canUseAssignFaststrType(key *runtime.Type, value *runtime.Type) bool {
indirectElem := value.Size() > mapMaxElemSize
if indirectElem {
return false
}
return key.Kind() == reflect.String
}
//go:linkname makemap reflect.makemap
func makemap(*runtime.Type, int) unsafe.Pointer