Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-4678

Parsing `[ { $code: ... } ]` or `[{$dbPointer: ...}]` results in unexpected key

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 1.24.3
    • Affects Version/s: None
    • Component/s: None
    • None

      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);
         }
      

            Assignee:
            josh.siegel@mongodb.com Josh Siegel
            Reporter:
            kevin.albertson@mongodb.com Kevin Albertson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: