Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-802

Document fields corrupted in transformAndEnsureID

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.0.0-rc1
    • Affects Version/s: None
    • Component/s: CRUD
    • None

      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

       

            Assignee:
            isabella.siu@mongodb.com Isabella Siu (Inactive)
            Reporter:
            softzilla Pavel Antonov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: