Skip to content

Commit a31ef4a

Browse files
committed
Merge branch 'master' into 3.1-dev
2 parents abf15a9 + 7a8b689 commit a31ef4a

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/mongocxx/collection.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,22 @@ mongocxx::stdx::optional<bsoncxx::document::value> find_and_modify(
6666
const ::mongoc_find_and_modify_opts_t* opts) {
6767
mongocxx::libbson::scoped_bson_t bson_filter{filter};
6868
mongocxx::libbson::scoped_bson_t reply;
69-
reply.flag_init();
69+
70+
// TODO: There is a bug in the 1.3 vintage C driver where collection_find_and_modify_with_opts
71+
// does not always initialize its 'reply' parameter in the event of errors. So, explicitly init
72+
// the underlying bson_t here. This can be changed to ''flag_init' once the C driver minimum is
73+
// bumped to or past 1.4.0.
74+
reply.init();
7075

7176
::bson_error_t error;
7277

7378
bool r = mongocxx::libmongoc::collection_find_and_modify_with_opts(
7479
collection, bson_filter.bson(), opts, reply.bson(), &error);
7580

7681
if (!r) {
77-
auto gle = mongocxx::libmongoc::collection_get_last_error(collection);
78-
mongocxx::throw_exception<mongocxx::write_exception>(
79-
bsoncxx::helpers::value_from_bson_t(gle), error);
82+
if (!reply.view().empty())
83+
mongocxx::throw_exception<mongocxx::write_exception>(reply.steal(), error);
84+
mongocxx::throw_exception<mongocxx::write_exception>(error);
8085
}
8186

8287
bsoncxx::document::view result = reply.view();

src/mongocxx/test/collection.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,11 @@ TEST_CASE("create_index returns index name", "[collection]") {
702702
auto response2 = coll.create_index(index2.view(), options::index{});
703703
REQUIRE(response2.view()["name"].get_utf8().value == stdx::string_view{"b_1_c_-1"});
704704
}
705+
706+
TEST_CASE("regressions", "CXX-986") {
707+
instance::current();
708+
mongocxx::uri mongo_uri{"mongodb://non-existent-host.invalid/"};
709+
mongocxx::client client{mongo_uri};
710+
REQUIRE_THROWS(client.database("irrelevant")["irrelevant"].find_one_and_update(
711+
document{} << "irrelevant" << 1 << finalize, document{} << "irrelevant" << 2 << finalize));
712+
}

0 commit comments

Comments
 (0)