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

Invalid error handling in find_and_modify - null pointer access

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 3.0.1
    • Fix Version/s: 3.0.2
    • Component/s: Implementation
    • Labels:
      None
    • Sprint:
      Platforms 2016-09-19

      Description

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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              acm Andrew Morrow
              Reporter:
              rustyx Rustam Abdullaev
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: