Skip to content

Commit 46119b4

Browse files
GODRIVER-3434 Avoid initializing null data given custom decoder (#1903)
1 parent 17a547f commit 46119b4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

bson/default_value_decoders.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,12 @@ func valueUnmarshalerDecodeValue(_ DecodeContext, vr ValueReader, val reflect.Va
11661166
return ValueDecoderError{Name: "ValueUnmarshalerDecodeValue", Types: []reflect.Type{tValueUnmarshaler}, Received: val}
11671167
}
11681168

1169+
if vr.Type() == TypeNull {
1170+
val.Set(reflect.Zero(val.Type()))
1171+
1172+
return vr.ReadNull()
1173+
}
1174+
11691175
if val.Kind() == reflect.Ptr && val.IsNil() {
11701176
if !val.CanSet() {
11711177
return ValueDecoderError{Name: "ValueUnmarshalerDecodeValue", Types: []reflect.Type{tValueUnmarshaler}, Received: val}

bson/unmarshaling_cases_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,25 @@ type unmarshalerNonPtrStruct struct {
197197

198198
type myInt64 int64
199199

200+
var _ ValueUnmarshaler = (*myInt64)(nil)
201+
202+
func (mi *myInt64) UnmarshalBSONValue(t byte, b []byte) error {
203+
if len(b) == 0 {
204+
return nil
205+
}
206+
207+
if Type(t) == TypeInt64 {
208+
i, err := newValueReader(TypeInt64, bytes.NewReader(b)).ReadInt64()
209+
if err != nil {
210+
return err
211+
}
212+
213+
*mi = myInt64(i)
214+
}
215+
216+
return nil
217+
}
218+
200219
func (mi *myInt64) UnmarshalBSON(b []byte) error {
201220
if len(b) == 0 {
202221
return nil

0 commit comments

Comments
 (0)