[CDRIVER-2755] Inconsistent handling of empty keys in iterator functions Created: 18/Jul/18  Updated: 28/Oct/23  Resolved: 18/Jul/18

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

Type: Bug Priority: Major - P3
Reporter: Kevin Albertson Assignee: Kevin Albertson
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CXX-1500 document::view::find should use bson_... Closed
Backwards Compatibility: Minor Change

 Description   

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.



 Comments   
Comment by Githook User [ 18/Jul/18 ]

Author:

{'username': 'kevinAlbs', 'name': 'Kevin Albertson', 'email': 'kevin.albertson@10gen.com'}

Message: CDRIVER-2755 all find funcs find empty keys
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/1c73768712399eb8c7de35dda9f7babeb3002f10

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