Description
If document contains binary fields at the beginning of structure, data in fields will be overwritten with "_id" and corrupted.
Code:
client, _ := mongo.NewClient("mongodb://localhost:27017") |
client.Connect(context.TODO())
|
db := client.Database("test") |
|
|
type D struct {
|
Bin []byte `bson:"bin"` |
ID string `bson:"_id"` |
}
|
d := &D{
|
Bin: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
ID: "LongEnoughIdentifier", |
}
|
|
|
_, err := db.Collection("test").InsertOne(context.TODO(), d) |
As result "bin" field will contains []byte("Identifier") instead of zeros.
Cause of error, using same buffer (var buf) for document data values and _id encoding/decoding. Here: https://github.com/mongodb/mongo-go-driver/blob/42e68e39d0196d841aed31ec9e40e80edb4f1566/mongo/mongo.go#L120