[CDRIVER-4678] Parsing `[ { $code: ... } ]` or `[{$dbPointer: ...}]` results in unexpected key Created: 12/Jul/23  Updated: 28/Oct/23  Resolved: 26/Jul/23

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 1.24.3

Type: Bug Priority: Unknown
Reporter: Kevin Albertson Assignee: Josh Siegel (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

Parsing

 [{"$code" : "A"}]

results in BSON data with an unexpected "\xbe" key:

% echo "0E0000000DBE0002000000410000" | ~/code/specifications/source/bson-corpus/tests/bsonview -x
 0e000000 0d "\xbe" 00 02000000 "A" 00 00

The same"\xbe" key appears for parsing $dbPointer.

The expected behavior is to parse to an array document with the key "0". The expected parsed BSON is to be equivalent to parsing:

{"0" : {"$code" : "A"}} 

results in BSON data with the expected "0" key:

% echo "0E0000000D300002000000410000" | ~/code/specifications/source/bson-corpus/tests/bsonview -x
 0e000000 0d "0" 00 02000000 "A" 00 00

Environment

libbson 1.24.1 on macOS 12.6.7 arm64. Clang 16.0.5.

How to Reproduce

Here is a branch with a failing test: https://github.com/mongodb/mongo-c-driver/compare/master...kevinAlbs:mongo-c-driver:code_parsing_bug?expand=1 :

   // Test parsing `[ {"$code" : "A"} ]` and `{ "0": {"$code" : "A"} }`. Results
   // in incorrect unequal BSON.
   {
      bson_t *b1;
      {
         // Parsing `[ {"$code" : "A"} ]` results in an invalid key.
         const char *json = BSON_STR ([ {"$code" : "A"} ]);
         bson_error_t error;
         b1 = bson_new_from_json ((const uint8_t *) json, -1, &error);
         ASSERT_OR_PRINT (b1, error);
         printf ("bson parsed from %s:\n", json);
         dump_bson (b1);
      }
 
      bson_t *b2;
      {
         const char *json = BSON_STR ({"0" : {"$code" : "A"}});
         bson_error_t error;
         b2 = bson_new_from_json ((const uint8_t *) json, -1, &error);
         ASSERT_OR_PRINT (b2, error);
         printf ("bson parsed from %s:\n", json);
         dump_bson (b2);
      }
 
      ASSERT (bson_equal (b1, b2));
      bson_destroy (b2);
      bson_destroy (b1);
   }



 Comments   
Comment by Githook User [ 28/Jul/23 ]

Author:

{'name': 'Joshua Siegel', 'email': '39130209+joshbsiegel@users.noreply.github.com', 'username': 'joshbsiegel'}

Message: CDRIVER-4678 fix `$code` and `$dbPointer` array parsing (#1356)
Branch: r1.24
https://github.com/mongodb/mongo-c-driver/commit/d432311dfe0a6331e2d2dfd6ab1f5428004c07da

Comment by Githook User [ 26/Jul/23 ]

Author:

{'name': 'Joshua Siegel', 'email': '39130209+joshbsiegel@users.noreply.github.com', 'username': 'joshbsiegel'}

Message: CDRIVER-4678 fix `$code` and `$dbPointer` array parsing (#1356)
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/81631fda7bc57b248d3982bde1a2b2ee82803ad4

Generated at Wed Feb 07 21:21:36 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.