Skip to content

Commit bff8804

Browse files
aseeringsaghm
authored andcommitted
CXX-1187 Handle null return values from libmongoc for uri's
Closes #581
1 parent 7bd6dcd commit bff8804

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/mongocxx/test/uri.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@ TEST_CASE("URI", "[uri]") {
2626
REQUIRE_NOTHROW(mongocxx::uri{mongocxx::uri::k_default_uri});
2727
REQUIRE(mongocxx::uri{}.to_string() ==
2828
mongocxx::uri{mongocxx::uri::k_default_uri}.to_string());
29+
30+
mongocxx::uri u{};
31+
32+
// Values that should be empty with a blank URI.
33+
REQUIRE(u.auth_mechanism() == "");
34+
REQUIRE(u.auth_source() == "admin");
35+
REQUIRE(u.database() == "");
36+
REQUIRE(u.hosts().size() == 1);
37+
REQUIRE(u.hosts()[0].name == "localhost");
38+
// Don't check 'u.hosts()[0].family'. Value is platform-dependent.
39+
REQUIRE(u.hosts()[0].port == 27017);
40+
REQUIRE(u.options().empty());
41+
REQUIRE(u.password() == "");
42+
REQUIRE(u.read_concern().acknowledge_level() == mongocxx::read_concern::level::k_server_default);
43+
REQUIRE(u.read_concern().acknowledge_string().empty());
44+
REQUIRE(u.read_preference().mode() == mongocxx::read_preference::read_mode::k_primary);
45+
REQUIRE(!u.read_preference().tags());
46+
REQUIRE(!u.read_preference().max_staleness());
47+
REQUIRE(u.replica_set() == "");
48+
REQUIRE(u.ssl() == false);
49+
REQUIRE(u.to_string() == mongocxx::uri::k_default_uri);
50+
REQUIRE(u.username() == "");
51+
REQUIRE(u.write_concern().journal() == false);
52+
REQUIRE(u.write_concern().majority() == false);
53+
REQUIRE(!u.write_concern().nodes());
54+
REQUIRE(u.write_concern().timeout() == std::chrono::milliseconds{0});
55+
REQUIRE(u.write_concern().acknowledge_level());
56+
REQUIRE(*u.write_concern().acknowledge_level() == mongocxx::write_concern::level::k_default);
2957
}
3058

3159
SECTION("Valid URI") {

src/mongocxx/uri.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@
2828
namespace mongocxx {
2929
MONGOCXX_INLINE_NAMESPACE_BEGIN
3030

31+
namespace {
32+
33+
// Some of the 'uri_get_*' string accessors may return nullptr.
34+
// Check for this case and convert to the empty string.
35+
std::string to_string_null_safe(const char* str) {
36+
if (str == nullptr) {
37+
return std::string{};
38+
}
39+
return str;
40+
}
41+
42+
} // namespace
43+
3144
const std::string uri::k_default_uri = "mongodb://localhost:27017";
3245

3346
uri::uri(std::unique_ptr<impl>&& implementation) {
@@ -47,15 +60,15 @@ uri& uri::operator=(uri&&) noexcept = default;
4760
uri::~uri() = default;
4861

4962
std::string uri::auth_mechanism() const {
50-
return libmongoc::uri_get_auth_mechanism(_impl->uri_t);
63+
return to_string_null_safe(libmongoc::uri_get_auth_mechanism(_impl->uri_t));
5164
}
5265

5366
std::string uri::auth_source() const {
5467
return libmongoc::uri_get_auth_source(_impl->uri_t);
5568
}
5669

5770
std::string uri::database() const {
58-
return libmongoc::uri_get_database(_impl->uri_t);
71+
return to_string_null_safe(libmongoc::uri_get_database(_impl->uri_t));
5972
}
6073

6174
std::vector<uri::host> uri::hosts() const {
@@ -75,7 +88,7 @@ bsoncxx::document::view uri::options() const {
7588
}
7689

7790
std::string uri::password() const {
78-
return libmongoc::uri_get_password(_impl->uri_t);
91+
return to_string_null_safe(libmongoc::uri_get_password(_impl->uri_t));
7992
}
8093

8194
class read_concern uri::read_concern() const {
@@ -91,7 +104,7 @@ class read_preference uri::read_preference() const {
91104
}
92105

93106
std::string uri::replica_set() const {
94-
return libmongoc::uri_get_replica_set(_impl->uri_t);
107+
return to_string_null_safe(libmongoc::uri_get_replica_set(_impl->uri_t));
95108
}
96109

97110
std::string uri::to_string() const {
@@ -103,7 +116,7 @@ bool uri::ssl() const {
103116
}
104117

105118
std::string uri::username() const {
106-
return libmongoc::uri_get_username(_impl->uri_t);
119+
return to_string_null_safe(libmongoc::uri_get_username(_impl->uri_t));
107120
}
108121

109122
class write_concern uri::write_concern() const {

0 commit comments

Comments
 (0)