[CXX-1511] Client constructor should throw if mongoc_uri_new_with_error fails Created: 05/Feb/18  Updated: 28/Oct/23  Resolved: 01/Mar/18

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 3.3.0-rc0

Type: Bug Priority: Minor - P4
Reporter: Matt Broadstone Assignee: A. Jesse Jiryu Davis
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

It is possible for mongoc_uri_new_with_error to return NULL, and we are completely ignoring this fact during construction of mongocxx::client. It's very unlikely that this would happen, as most of the paths that would return an error would be covered during construction of the mongoc_uri_t passed in, but we should throw nonetheless in the unlikely case that this does happen.



 Comments   
Comment by Githook User [ 02/Mar/18 ]

Author:

{'email': 'jesse@mongodb.com', 'name': 'A. Jesse Jiryu Davis', 'username': 'ajdavis'}

Message: CXX-1511 Check mongoc_client_new_with_uri return
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/2337f20c2d37d3d4ab59ece05004f4c5d2833a86

Comment by Matt Broadstone [ 24/Feb/18 ]

The constructor for client uses libmongoc::client_new_from_uri, which returns a mongoc_client_t *. I checked the implementation, and it's true that we do not, in fact, return NULL in any case - so if you are committed to making sure we never ever return NULL from that function then this is safe, otherwise we should check that the result is non-null and throw an exception.

Comment by A. Jesse Jiryu Davis [ 23/Feb/18 ]

Don't think this is a bug. The driver's uri class is constructed like:

uri::uri(bsoncxx::string::view_or_value uri_string)
    : _impl(stdx::make_unique<impl>(libmongoc::uri_new(uri_string.terminated().data()))) {
    if (_impl->uri_t == nullptr) {
        throw logic_error{error_code::k_invalid_uri};
    }
}

... and the client constructor takes a uri instance. What path leads us to construct a client with a NULL mongoc_uri_t pointer?

Generated at Wed Feb 07 22:02:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.