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

crash with bson_iter_find_descendant and array of documents

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.6.2
    • Component/s: libbson
    • Labels:
      None
    • Environment:
      Windows 10 64bits

      Working on the following collection:

      {"_id":{"$oid":"58e17a4dd7b07d1fee077fac"},"item":"journal","instock":[{"warehouse":"A","qty":5.0},{"warehouse":"C","qty":15.0}]}
      {"_id":{"$oid":"58e17a4dd7b07d1fee077fad"},"item":"notebook","instock":[{"warehouse":"C","qty":5.0}]}
      {"_id":{"$oid":"58e17a4dd7b07d1fee077fae"},"item":"paper","instock":[{"warehouse":"A","qty":60.0},{"warehouse":"B","qty":15.0}]}
      {"_id":{"$oid":"58e17a4dd7b07d1fee077faf"},"item":"planner","instock":[{"warehouse":"A","qty":40.0},{"warehouse":"B","qty":5.0}]}
      {"_id":{"$oid":"58e17a4dd7b07d1fee077fb0"},"item":"postcard","instock":[{"warehouse":"B","qty":15.0},{"warehouse":"C","qty":35.0}]}
      

      When finding the "instock" item by:

      bson_iter_find_descendant(&Iter, "instock", &Desc))
      

      it is correctly of type ARRAY and can be printed with:

      if (BSON_ITER_HOLDS_ARRAY(&Iter)) {
        char *str = NULL;
        bson_t *arr;
        const uint8_t *data = NULL;
        uint32_t len = 0;
      
        bson_iter_array(&Desc, &len, &data);
        arr = bson_new_from_data(data, len);
        str = bson_as_json(arr, NULL);
        bson_free(str);
        bson_destroy(arr);
      

      str is:

      {"0":{"warehouse":"A","qty":5.0},"1":{"warehouse":"C","qty":15.0}}
      {"0":{"warehouse":"C","qty":5.0}}
      {"0":{"warehouse":"A","qty":60.0},"1":{"warehouse":"B","qty":15.0}}
      {"0":{"warehouse":"A","qty":40.0},"1":{"warehouse":"B","qty":5.0}}
      {"0":{"warehouse":"B","qty":15.0},"1":{"warehouse":"C","qty":35.0}}
      

      It is also possible to retrieve the values of wareahouse or qty by something like:

      bson_iter_find_descendant(&Iter, "instock.0.warehouse", &Desc))
      or
      bson_iter_find_descendant(&Iter, "instock.1.qty", &Desc))
      

      However when using:

      bson_iter_find_descendant(&Iter, "instock.0", &Desc))
      

      One would expect to retrieve an item of type DOCUMENT. But instead it is retrieved as type ARRAY and when trying to execute:

      bson_iter_document(&Desc, &len, &data);
      arr = bson_new_from_data(data, len);
      

      data is null and the program crashes.

            Assignee:
            bjori Hannes Magnusson
            Reporter:
            Buggynours Olivier Bertrand [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: