Skip to content

Commit 208355d

Browse files
jmikolahanumantmk
authored andcommitted
CDRIVER-581: Don't assume write concern defaults in URI parsing
False safe/journal values should be explicitly set on the write concern. Additionally, w=1 should not be parsed as MONGOC_WRITE_CONCERN_W_DEFAULT, since the driver would then omit it from the write concern and allow a different server-side default to be applied. Closes #195
1 parent 9c01ab7 commit 208355d

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/mongoc/mongoc-uri.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -717,10 +717,9 @@ _mongoc_uri_build_write_concern (mongoc_uri_t *uri) /* IN */
717717
write_concern = mongoc_write_concern_new ();
718718

719719
if (bson_iter_init_find_case (&iter, &uri->options, "safe") &&
720-
BSON_ITER_HOLDS_BOOL (&iter) &&
721-
!bson_iter_bool (&iter)) {
720+
BSON_ITER_HOLDS_BOOL (&iter)) {
722721
mongoc_write_concern_set_w (write_concern,
723-
MONGOC_WRITE_CONCERN_W_UNACKNOWLEDGED);
722+
bson_iter_bool (&iter) ? 1 : MONGOC_WRITE_CONCERN_W_UNACKNOWLEDGED);
724723
}
725724

726725
if (bson_iter_init_find_case (&iter, &uri->options, "wtimeoutms") &&
@@ -729,9 +728,8 @@ _mongoc_uri_build_write_concern (mongoc_uri_t *uri) /* IN */
729728
}
730729

731730
if (bson_iter_init_find_case (&iter, &uri->options, "journal") &&
732-
BSON_ITER_HOLDS_BOOL (&iter) &&
733-
bson_iter_bool (&iter)) {
734-
mongoc_write_concern_set_journal (write_concern, true);
731+
BSON_ITER_HOLDS_BOOL (&iter)) {
732+
mongoc_write_concern_set_journal (write_concern, bson_iter_bool (&iter));
735733
}
736734

737735
if (bson_iter_init_find_case (&iter, &uri->options, "w")) {
@@ -747,12 +745,8 @@ _mongoc_uri_build_write_concern (mongoc_uri_t *uri) /* IN */
747745
mongoc_write_concern_set_w (write_concern,
748746
MONGOC_WRITE_CONCERN_W_UNACKNOWLEDGED);
749747
break;
750-
case 1:
751-
mongoc_write_concern_set_w (write_concern,
752-
MONGOC_WRITE_CONCERN_W_DEFAULT);
753-
break;
754748
default:
755-
if (value > 1) {
749+
if (value > 0) {
756750
mongoc_write_concern_set_w (write_concern, value);
757751
break;
758752
}

tests/test-mongoc-uri.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ test_mongoc_uri_new (void)
132132
ASSERT(!bson_iter_next(&iter));
133133
mongoc_uri_destroy(uri);
134134

135+
uri = mongoc_uri_new("mongodb://localhost/?safe=false&journal=false");
136+
options = mongoc_uri_get_options(uri);
137+
ASSERT(options);
138+
ASSERT_CMPINT(bson_count_keys(options), ==, 2);
139+
ASSERT(bson_iter_init(&iter, options));
140+
ASSERT(bson_iter_find_case(&iter, "safe"));
141+
ASSERT(BSON_ITER_HOLDS_BOOL(&iter));
142+
ASSERT(!bson_iter_bool(&iter));
143+
ASSERT(bson_iter_find_case(&iter, "journal"));
144+
ASSERT(BSON_ITER_HOLDS_BOOL(&iter));
145+
ASSERT(!bson_iter_bool(&iter));
146+
ASSERT(!bson_iter_next(&iter));
147+
mongoc_uri_destroy(uri);
148+
135149
uri = mongoc_uri_new("mongodb:///tmp/mongodb-27017.sock/?ssl=false");
136150
ASSERT(uri);
137151
ASSERT_CMPSTR(mongoc_uri_get_hosts(uri)->host, "/tmp/mongodb-27017.sock");
@@ -413,17 +427,17 @@ test_mongoc_uri_write_concern (void)
413427
int i;
414428
static const write_concern_test tests [] = {
415429
{ "mongodb://localhost/?safe=false", true, MONGOC_WRITE_CONCERN_W_UNACKNOWLEDGED },
416-
{ "mongodb://localhost/?safe=true", true, MONGOC_WRITE_CONCERN_W_DEFAULT },
430+
{ "mongodb://localhost/?safe=true", true, 1 },
417431
{ "mongodb://localhost/?w=-1", true, MONGOC_WRITE_CONCERN_W_ERRORS_IGNORED },
418432
{ "mongodb://localhost/?w=0", true, MONGOC_WRITE_CONCERN_W_UNACKNOWLEDGED },
419-
{ "mongodb://localhost/?w=1", true, MONGOC_WRITE_CONCERN_W_DEFAULT },
433+
{ "mongodb://localhost/?w=1", true, 1 },
420434
{ "mongodb://localhost/?w=2", true, 2 },
421435
{ "mongodb://localhost/?w=majority", true, MONGOC_WRITE_CONCERN_W_MAJORITY },
422436
{ "mongodb://localhost/?w=10", true, 10 },
423437
{ "mongodb://localhost/?w=", true, MONGOC_WRITE_CONCERN_W_DEFAULT },
424438
{ "mongodb://localhost/?w=mytag", true, MONGOC_WRITE_CONCERN_W_TAG, "mytag" },
425439
{ "mongodb://localhost/?w=mytag&safe=false", true, MONGOC_WRITE_CONCERN_W_TAG, "mytag" },
426-
{ "mongodb://localhost/?w=1&safe=false", true, MONGOC_WRITE_CONCERN_W_DEFAULT },
440+
{ "mongodb://localhost/?w=1&safe=false", true, 1 },
427441
{ NULL }
428442
};
429443

0 commit comments

Comments
 (0)