From abce6f6fa258eee230bc7d51ec6ea5ed02ea0e8b Mon Sep 17 00:00:00 2001 From: andot Date: Wed, 13 Sep 2023 14:58:39 +0800 Subject: [PATCH] fixed data race --- io/struct_decoder.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/io/struct_decoder.go b/io/struct_decoder.go index 23f6144..94df7d1 100644 --- a/io/struct_decoder.go +++ b/io/struct_decoder.go @@ -6,7 +6,7 @@ | | | io/struct_decoder.go | | | -| LastModified: Mar 5, 2022 | +| LastModified: Dec 13, 2023 | | Author: Ma Bingyao | | | \*________________________________________________________*/ @@ -67,10 +67,14 @@ func (dec *Decoder) ReadObject() interface{} { type structDecoder struct { t *reflect2.UnsafeStructType fields map[string]FieldAccessor + sync.RWMutex } func (valdec *structDecoder) decodeField(dec *Decoder, ptr unsafe.Pointer, name string) { - if field, ok := valdec.fields[name]; ok { + valdec.RLock() + field, ok := valdec.fields[name] + valdec.RUnlock() + if ok { field.Decode(dec, field.Type.Type1(), field.Field.UnsafeGet(ptr)) } else { var v interface{} @@ -130,6 +134,8 @@ func getNamedStructDecoder(t reflect.Type) ValueDecoder { func newNamedStructDecoder(t reflect.Type, tag ...string) *structDecoder { t2 := reflect2.Type2(t).(*reflect2.UnsafeStructType) decoder := &structDecoder{t: t2} + decoder.Lock() + defer decoder.Unlock() registerNamedStructDecoder(t, decoder) decoder.fields = getFieldMap(t, tag...) return decoder