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

Inconsistent handling of empty keys in iterator functions

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.13.0
    • Affects Version/s: None
    • Component/s: libbson
    • None
    • Minor Change

      Our iter functions handle empty keys in different ways.

      • bson_iter_init_find finds empty keys
      • bson_iter_init_find_w_len does not find empty keys (i.e. trying to find by passing length 0 won't find an empty key in a document)
      • bson_iter_find_descendant can't descend into empty keys

      Example below:

      static void
      test_bson_iter_empty_key (void) {
         /* create the bson document { "" : { "x" : 1 } } */
         bson_t* bson = BCON_NEW("", "{", "x", BCON_INT32(1), "}");
         bson_iter_t iter;
         bson_iter_t descendant;
      
         /* a find works, because internally it passes -1 to bson_iter_init_find_w_len */
         BSON_ASSERT(bson_iter_init_find (&iter, bson, ""));
         BSON_ASSERT(BSON_ITER_HOLDS_DOCUMENT (&iter));
         /* a find_w_len and length 0 does *not* work, because we return early. */
         BSON_ASSERT(!bson_iter_init_find_w_len (&iter, bson, "", 0));
         /* similarly, we can't descend if we've iterated on an empty key. */
         bson_iter_init (&iter, bson);
         BSON_ASSERT(!bson_iter_find_descendant (&iter, ".x", &descendant));
      }
      

      Note, this wasn't introduced in CDRIVER-2414. bson_iter_find_descendant had the same behavior.

      I propose we change bson_iter_find_descendant and bson_iter_init_find_w_len to find empty keys. This also makes it possible to do CXX-1500 and keep behavior of finding empty keys in the C++ driver.

            Assignee:
            kevin.albertson@mongodb.com Kevin Albertson
            Reporter:
            kevin.albertson@mongodb.com Kevin Albertson
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: