Skip to content

Commit f4cb748

Browse files
committed
CDRIVER-851: Negative wtimeout values are invalid
1 parent 7415d34 commit f4cb748

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/mongoc/mongoc-write-concern.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,21 @@ mongoc_write_concern_get_wtimeout (const mongoc_write_concern_t *write_concern)
198198
* @wtimeout_msec: Number of milliseconds before timeout.
199199
*
200200
* Sets the number of milliseconds to wait before considering a write
201-
* request as failed.
201+
* request as failed. A value of 0 indicates no write timeout.
202+
*
203+
* The @wtimeout_msec parameter must be positive or zero. Negative values will
204+
* be ignored.
202205
*/
203206
void
204207
mongoc_write_concern_set_wtimeout (mongoc_write_concern_t *write_concern,
205208
int32_t wtimeout_msec)
206209
{
207210
BSON_ASSERT (write_concern);
208211

212+
if (wtimeout_msec < 0) {
213+
return;
214+
}
215+
209216
if (!_mongoc_write_concern_warn_frozen(write_concern)) {
210217
write_concern->wtimeout = wtimeout_msec;
211218
}
@@ -227,7 +234,10 @@ mongoc_write_concern_get_wmajority (const mongoc_write_concern_t *write_concern)
227234
*
228235
* Sets the "w" of a write concern to "majority". It is suggested that
229236
* you provide a reasonable @wtimeout_msec to wait before considering the
230-
* write request failed.
237+
* write request failed. A @wtimeout_msec value of 0 indicates no write timeout.
238+
*
239+
* The @wtimeout_msec parameter must be positive or zero. Negative values will
240+
* be ignored.
231241
*/
232242
void
233243
mongoc_write_concern_set_wmajority (mongoc_write_concern_t *write_concern,
@@ -237,7 +247,10 @@ mongoc_write_concern_set_wmajority (mongoc_write_concern_t *write_concern,
237247

238248
if (!_mongoc_write_concern_warn_frozen(write_concern)) {
239249
write_concern->w = MONGOC_WRITE_CONCERN_W_MAJORITY;
240-
write_concern->wtimeout = wtimeout_msec;
250+
251+
if (wtimeout_msec >= 0) {
252+
write_concern->wtimeout = wtimeout_msec;
253+
}
241254
}
242255
}
243256

@@ -415,5 +428,9 @@ _mongoc_write_concern_is_valid (const mongoc_write_concern_t *write_concern)
415428
return false;
416429
}
417430

431+
if (write_concern->wtimeout < 0) {
432+
return false;
433+
}
434+
418435
return true;
419436
}

tests/test-mongoc-write-concern.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,38 @@ test_write_concern_fsync_and_journal_gle_and_validity (void)
207207
mongoc_write_concern_destroy(write_concern);
208208
}
209209

210+
static void
211+
test_write_concern_wtimeout_validity (void)
212+
{
213+
mongoc_write_concern_t *write_concern = mongoc_write_concern_new();
214+
215+
/* Test defaults */
216+
ASSERT(write_concern);
217+
ASSERT(mongoc_write_concern_get_w(write_concern) == MONGOC_WRITE_CONCERN_W_DEFAULT);
218+
ASSERT(mongoc_write_concern_get_wtimeout(write_concern) == 0);
219+
ASSERT(!mongoc_write_concern_get_wmajority(write_concern));
220+
221+
/* mongoc_write_concern_set_wtimeout() ignores invalid wtimeout */
222+
mongoc_write_concern_set_wtimeout(write_concern, -1);
223+
ASSERT(mongoc_write_concern_get_w(write_concern) == MONGOC_WRITE_CONCERN_W_DEFAULT);
224+
ASSERT(mongoc_write_concern_get_wtimeout(write_concern) == 0);
225+
ASSERT(!mongoc_write_concern_get_wmajority(write_concern));
226+
ASSERT(_mongoc_write_concern_is_valid(write_concern));
227+
228+
/* mongoc_write_concern_set_wmajority() ignores invalid wtimeout */
229+
mongoc_write_concern_set_wmajority(write_concern, -1);
230+
ASSERT(mongoc_write_concern_get_w(write_concern) == MONGOC_WRITE_CONCERN_W_MAJORITY);
231+
ASSERT(mongoc_write_concern_get_wtimeout(write_concern) == 0);
232+
ASSERT(mongoc_write_concern_get_wmajority(write_concern));
233+
ASSERT(_mongoc_write_concern_is_valid(write_concern));
234+
235+
/* Manually assigning a negative wtimeout will make the write concern invalid */
236+
write_concern->wtimeout = -1;
237+
ASSERT(!_mongoc_write_concern_is_valid(write_concern));
238+
239+
mongoc_write_concern_destroy(write_concern);
240+
}
241+
210242

211243
void
212244
test_write_concern_install (TestSuite *suite)
@@ -215,4 +247,5 @@ test_write_concern_install (TestSuite *suite)
215247
TestSuite_Add (suite, "/WriteConcern/bson_omits_defaults", test_write_concern_bson_omits_defaults);
216248
TestSuite_Add (suite, "/WriteConcern/bson_includes_false_fsync_and_journal", test_write_concern_bson_includes_false_fsync_and_journal);
217249
TestSuite_Add (suite, "/WriteConcern/fsync_and_journal_gle_and_validity", test_write_concern_fsync_and_journal_gle_and_validity);
250+
TestSuite_Add (suite, "/WriteConcern/wtimeout_validity", test_write_concern_wtimeout_validity);
218251
}

0 commit comments

Comments
 (0)