[GODRIVER-2896] empty bson.RawValue marshal unmarshal behavior are inconsistent. unmarshal error Created: 11/Jul/23  Updated: 28/Oct/23  Resolved: 28/Jul/23

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

Type: Bug Priority: Critical - P2
Reporter: mongo mongo Assignee: Preston Vasquez
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
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   

Summary

when i read a mongo struct from mongodb, then process some filed, marshal to bytes, then store to cache.

when  other scenes, get bytes from cache, then unmarshal it , it is not work.

like this: 

unmarshal error document is invalid, end byte is at 28, but null byte found at 21

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

the latest  version  v1.12.0 still has the same bug

How to Reproduce

type P struct

{   Name string `bson:"name,omitempty"`   Likes bson.RawValue `bson:"likes,omitempty"` }

func TestMismatch(t *testing.T)

{   bytes, err := bson.Marshal(&P\{Name: "lilei"}

)
  if err != nil

{       t.Fatalf("marshal error %s", err)   }

  var p P
  err = bson.Unmarshal(bytes, &p)
  if err != nil

{      t.Fatalf("unmarshal error %s", err)   }

}

Additional Background



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

Author:

{'name': 'Preston Vasquez', 'email': 'prestonvasquez@icloud.com', 'username': 'prestonvasquez'}

Message: GODRIVER-2896 Add IsZero to BSON RawValue (#1332)

Co-authored-by: Matt Dale <9760375+matthewdale@users.noreply.github.com>
Branch: release/1.12
https://github.com/mongodb/mongo-go-driver/commit/6c3474948935af10dc57388763d12de6837f1e3a

Comment by Githook User [ 28/Jul/23 ]

Author:

{'name': 'Preston Vasquez', 'email': 'prestonvasquez@icloud.com', 'username': 'prestonvasquez'}

Message: GODRIVER-2896 Add IsZero to BSON RawValue (#1332)

Co-authored-by: Matt Dale <9760375+matthewdale@users.noreply.github.com>
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/215d683afcf2d95b8414f46f82f6a8e7dac688b1

Comment by mongo mongo [ 11/Jul/23 ]

sorry for  not familiar with the Jira editor, some mistake after i paste the code here.

I look into the mongo go driver, it may be the bug of  RawValueEncodeValue. 

 

When marshalling, an empty RawValue was encoded with type 0 and the filed‘s name written into the bytes, followed by the normal encoding of other fields.

During decoding, when encountering a type of 0, it is assumed to be a null terminator and no further fields should exist. However, since the name and additional fields were added during encoding, causing a mismatch in length, an error occurred.

Comment by PM Bot [ 11/Jul/23 ]

Hi 1124210681@qq.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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