Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-986

Invalid error handling in find_and_modify - null pointer access

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.0.2
    • Affects Version/s: 3.0.1
    • Component/s: Implementation
    • None

      This problem is related to this code in find_and_modify:

          bool r = mongocxx::libmongoc::collection_find_and_modify_with_opts(
              collection, bson_filter.bson(), opts, reply.bson(), &error);
      
          if (!r) {
              auto gle = mongocxx::libmongoc::collection_get_last_error(collection);
              mongocxx::throw_exception<mongocxx::write_exception>(
                  bsoncxx::helpers::value_from_bson_t(gle), error);
          }
      

      If the mongo operation fails at connection level, gle is null, causing null pointer access inside value_from_bson_t(gle).

      Test case:

      #include <iostream>
      #include <mongocxx.h>
      using bsoncxx::builder::stream::document;
      using bsoncxx::builder::stream::finalize;
      
      int main() {
        mongocxx::instance inst;
        try
        {
          mongocxx::uri mongo_uri{ "mongodb://non-existent-host/" };
          mongocxx::client client{ mongo_uri };
          client.database("irrelevant")["irrelevant"].find_one_and_update(
            document{} << "irrelevant" << 1 << finalize,
            document{} << "irrelevant" << 2 << finalize);
          std::cout << "OK" << std::endl;
        }
        catch (const std::exception& ex)
        {
          std::cerr << "Exception: " << ex.what() << std::endl;
        }
      }
      

            Assignee:
            andrew.morrow@mongodb.com Andrew Morrow (Inactive)
            Reporter:
            rustyx Rustam Abdullaev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: