[GODRIVER-2822] ReadConcern and WriteConcern marshalers error when empty Created: 26/Apr/23  Updated: 28/Oct/23  Resolved: 27/Jul/23

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 1.12.1

Type: Bug Priority: Unknown
Reporter: Preston Vasquez Assignee: Qingyang Hu
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

readconcern.ReadConcern and writeconcern.WriteConcern marshalers do not handle the nil case and so the following code will result in a runtime error:

type Foo struct {
	ReadConcern *readconcern.ReadConcern
}
 
func main() {
	foo := &Foo{}
 
         _, err := bson.Marshal(foo)
	if err != nil {
		panic(err)
	}
}

 

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x1195d78]goroutine 1 [running]:
go.mongodb.org/mongo-driver/mongo/readconcern.(*ReadConcern).MarshalBSONValue(0x3?)
        /Users/preston.vasquez/Developer/mongo-go-driver/mongo/readconcern/readconcern.go:73 +0x18
reflect.Value.call({0x11b7b40?, 0xc000014690?, 0x0?}, {0x11d831c, 0x4}, {0x0, 0x0, 0x121bce0?})
        /usr/local/go/src/reflect/value.go:586 +0xb07
reflect.Value.Call({0x11b7b40?, 0xc000014690?, 0x11b7b40?}, {0x0?, 0x11b7b40?, 0x1059b65?})
        /usr/local/go/src/reflect/value.go:370 +0xbc
go.mongodb.org/mongo-driver/bson/bsoncodec.DefaultValueEncoders.ValueMarshalerEncodeValue({}, {0xc0000061a0?, 0x50?}, {0x121b3f0, 0xc000036140}, {0x11b5f60?, 0xc000012048?, 0x11b5f60?})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/default_value_encoders.go:557 +0x2d1
go.mongodb.org/mongo-driver/bson/bsoncodec.ValueEncoderFunc.EncodeValue(0xc000036140?, {0xc00011cc40?, 0xb?}, {0x121b3f0?, 0xc000036140?}, {0x11b5f60?, 0xc000012048?, 0xc00000d2e0?})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/bsoncodec.go:178 +0x4a
go.mongodb.org/mongo-driver/bson/bsoncodec.(*StructCodec).EncodeValue(0x11a33a0?, {0xc00011cc40?, 0xc0?}, {0x121b3f0, 0xc000036140}, {0x11ba3c0?, 0xc000012048?, 0x500?})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/struct_codec.go:181 +0x5f6
go.mongodb.org/mongo-driver/bson/bsoncodec.(*PointerCodec).EncodeValue(0xc00007a750, {0xc00011cc40?, 0xa0?}, {0x121b3f0, 0xc000036140}, {0x11a33a0?, 0xc000012048?, 0x107a4f9?})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/bsoncodec/pointer_codec.go:67 +0x53b
go.mongodb.org/mongo-driver/bson.(*Encoder).Encode(0xc00006e1e0, {0x11a33a0?, 0xc000012048})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/encoder.go:79 +0x174
go.mongodb.org/mongo-driver/bson.MarshalAppendWithContext({0xc00011cc40?, 0x48?}, {0x136ac80, 0x0, 0x0}, {0x11a33a0, 0xc000012048})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/marshal.go:103 +0x21d
go.mongodb.org/mongo-driver/bson.MarshalAppendWithRegistry(...)
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/marshal.go:79
go.mongodb.org/mongo-driver/bson.MarshalWithRegistry(0xc0000061a0?, {0x11a33a0, 0xc000012048})
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/marshal.go:65 +0x52
go.mongodb.org/mongo-driver/bson.Marshal(...)
        /Users/preston.vasquez/Developer/mongo-go-driver/bson/marshal.go:51
main.main()
        /Users/preston.vasquez/Developer/technical/workshop/mongo/bson/marshal-client-options/main.go:17 +0x45
exit status 2 



 Comments   
Comment by Githook User [ 01/Aug/23 ]

Author:

{'name': 'Qingyang Hu', 'email': '103950869+qingyang-hu@users.noreply.github.com', 'username': 'qingyang-hu'}

Message: GODRIVER-2822 Add error on empty for ReadConcern marshaler. (#1327)
Branch: release/1.12
https://github.com/mongodb/mongo-go-driver/commit/b687278eeb70d57d0a210a91fb3e87bfe9edd714

Comment by Githook User [ 27/Jul/23 ]

Author:

{'name': 'Qingyang Hu', 'email': '103950869+qingyang-hu@users.noreply.github.com', 'username': 'qingyang-hu'}

Message: GODRIVER-2822 Add error on empty for ReadConcern marshaler. (#1327)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/6c5938979672af954559ef4ac8bbf9b9f7bea4d4

Comment by Qingyang Hu [ 19/Jul/23 ]

PR: https://github.com/mongodb/mongo-go-driver/pull/1327

Generated at Thu Feb 08 08:39:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.