Skip to content

Commit 6cef90a

Browse files
committed
CDRIVER-4781 Avoid intermediate uint32_t for socketTimeoutMS (#1475)
* Fix format specifier in timeout range validation error messages * CDRIVER-4781 Avoid overflow of -1 sockettimeout in cast to intermediate uint32_t * Add regression test for negative sockettimeoutms validation * Update documentation with warnings for non-positive timeout values * Update timeout variables for type consistency * Add regression test for unspecified default timeout in writev functions
1 parent 5e9ef21 commit 6cef90a

14 files changed

+191
-21
lines changed

src/libmongoc/doc/mongoc_stream_read.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Parameters
2626

2727
The :symbol:`mongoc_stream_read()` function shall perform a read from a :symbol:`mongoc_stream_t`. It's modeled on the API and semantics of ``read()``, though the parameters map only loosely.
2828

29+
.. warning::
30+
31+
The "default timeout" indicated by a negative value is both unspecified and
32+
unrelated to the documented default values for ``*TimeoutMS`` URI options.
33+
To specify a default timeout value for a ``*TimeoutMS`` URI option, use the
34+
``MONGOC_DEFAULT_*`` constants defined in ``mongoc-client.h``.
35+
2936
Returns
3037
-------
3138

@@ -38,4 +45,3 @@ The :symbol:`mongoc_stream_read` function returns the number of bytes read on su
3845
| :symbol:`mongoc_stream_write()`
3946
4047
| :symbol:`mongoc_stream_writev()`
41-

src/libmongoc/doc/mongoc_stream_readv.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Parameters
2626

2727
This function is identical to :symbol:`mongoc_stream_read()` except that it takes a :symbol:`mongoc_iovec_t` to perform gathered I/O.
2828

29+
.. warning::
30+
31+
The "default timeout" indicated by a negative value is both unspecified and
32+
unrelated to the documented default values for ``*TimeoutMS`` URI options.
33+
To specify a default timeout value for a ``*TimeoutMS`` URI option, use the
34+
``MONGOC_DEFAULT_*`` constants defined in ``mongoc-client.h``.
35+
2936
Returns
3037
-------
3138

@@ -38,4 +45,3 @@ Returns
3845
| :symbol:`mongoc_stream_write()`
3946
4047
| :symbol:`mongoc_stream_writev()`
41-

src/libmongoc/doc/mongoc_stream_write.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ Parameters
2424

2525
The :symbol:`mongoc_stream_write()` function shall perform a write to a :symbol:`mongoc_stream_t`. It's modeled on the API and semantics of ``write()``, though the parameters map only loosely.
2626

27+
.. warning::
28+
29+
The "default timeout" indicated by a negative value is both unspecified and
30+
unrelated to the documented default values for ``*TimeoutMS`` URI options.
31+
To specify a default timeout value for a ``*TimeoutMS`` URI option, use the
32+
``MONGOC_DEFAULT_*`` constants defined in ``mongoc-client.h``.
33+
2734
Returns
2835
-------
2936

@@ -36,4 +43,3 @@ The :symbol:`mongoc_stream_write` function returns the number of bytes written o
3643
| :symbol:`mongoc_stream_readv()`
3744
3845
| :symbol:`mongoc_stream_writev()`
39-

src/libmongoc/doc/mongoc_stream_writev.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ to a :symbol:`mongoc_stream_t`. It's modeled on the
2727
API and semantics of ``writev()``, though the parameters map only
2828
loosely.
2929

30+
.. warning::
31+
32+
The "default timeout" indicated by a negative value is both unspecified and
33+
unrelated to the documented default values for ``*TimeoutMS`` URI options.
34+
To specify a default timeout value for a ``*TimeoutMS`` URI option, use the
35+
``MONGOC_DEFAULT_*`` constants defined in ``mongoc-client.h``.
36+
3037
Returns
3138
-------
3239

3340
The number of bytes written on success, or ``-1`` upon failure and ``errno`` is set.
34-

src/libmongoc/doc/mongoc_uri_t.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ MONGOC_URI_LOADBALANCED loadbalanced fal
104104
MONGOC_URI_SRVMAXHOSTS srvmaxhosts 0 If zero, the number of hosts in DNS results is unlimited. If greater than zero, the number of hosts in DNS results is limited to being less than or equal to the given value.
105105
========================================== ================================= ================================= ============================================================================================================================================================================================================================================
106106

107-
Setting any of the \*timeoutMS options above to ``0`` will be interpreted as "use the default value".
107+
.. warning::
108+
109+
Setting any of the \*timeoutMS options above to either ``0`` or a negative value is discouraged due to unspecified and inconsistent behavior.
110+
The "default value" historically specified as a fallback for ``0`` or a negative value is NOT related to the default values for the \*timeoutMS options documented above.
111+
The meaning of a timeout of ``0`` or a negative value may vary depending on the operation being executed, even when specified by the same URI option.
112+
To specify the documented default value for a \*timeoutMS option, use the `MONGOC_DEFAULT_*` constants defined in ``mongoc-client.h`` instead.
108113

109114
Authentication Options
110115
----------------------
@@ -330,4 +335,3 @@ MONGOC_URI_SAFE safe {tr
330335
mongoc_uri_set_username
331336
mongoc_uri_set_write_concern
332337
mongoc_uri_unescape
333-

src/libmongoc/doc/mongoc_write_concern_set_wtimeout.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ instead of altering the write concern.
3131
.. seealso::
3232

3333
| :symbol:`mongoc_write_concern_get_wtimeout` and :symbol:`mongoc_write_concern_set_wtimeout_int64`.
34-

src/libmongoc/src/mongoc/mongoc-buffer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ _mongoc_buffer_append_from_stream (mongoc_buffer_t *buffer,
199199
bson_set_error (error,
200200
MONGOC_ERROR_STREAM,
201201
MONGOC_ERROR_STREAM_SOCKET,
202-
"timeout_msec value %" PRIu64
202+
"timeout_msec value %" PRId64
203203
" exceeds supported 32-bit range",
204204
timeout_msec);
205205
RETURN (false);
@@ -266,7 +266,7 @@ _mongoc_buffer_fill (mongoc_buffer_t *buffer,
266266
bson_set_error (error,
267267
MONGOC_ERROR_STREAM,
268268
MONGOC_ERROR_STREAM_SOCKET,
269-
"timeout_msec value %" PRIu64
269+
"timeout_msec value %" PRId64
270270
" exceeds supported 32-bit range",
271271
timeout_msec);
272272
RETURN (false);
@@ -342,7 +342,7 @@ _mongoc_buffer_try_append_from_stream (mongoc_buffer_t *buffer,
342342

343343
if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, timeout_msec))) {
344344
// CDRIVER-4589
345-
MONGOC_ERROR ("timeout_msec value %" PRIu64
345+
MONGOC_ERROR ("timeout_msec value %" PRId64
346346
" exceeds supported 32-bit range",
347347
timeout_msec);
348348
RETURN (-1);

src/libmongoc/src/mongoc/mongoc-cluster-private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ typedef struct _mongoc_cluster_node_t {
5353
typedef struct _mongoc_cluster_t {
5454
int64_t operation_id;
5555
int32_t request_id;
56-
uint32_t sockettimeoutms;
57-
uint32_t socketcheckintervalms;
56+
int32_t sockettimeoutms;
57+
int32_t socketcheckintervalms;
5858
mongoc_uri_t *uri;
5959
unsigned requires_auth : 1;
6060

src/libmongoc/src/mongoc/mongoc-crypt.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,7 @@ _state_need_kms (_state_machine_t *state_machine, bson_error_t *error)
491491
mongocrypt_binary_t *http_req = NULL;
492492
mongocrypt_binary_t *http_reply = NULL;
493493
const char *endpoint;
494-
uint32_t sockettimeout;
495-
496-
sockettimeout = MONGOC_DEFAULT_SOCKETTIMEOUTMS;
494+
const int32_t sockettimeout = MONGOC_DEFAULT_SOCKETTIMEOUTMS;
497495
kms_ctx = mongocrypt_ctx_next_kms_ctx (state_machine->ctx);
498496
while (kms_ctx) {
499497
mongoc_iovec_t iov;

src/libmongoc/src/mongoc/mongoc-secure-channel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ mongoc_secure_channel_read (mongoc_stream_tls_t *tls,
476476

477477
if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, tls->timeout_msec))) {
478478
// CDRIVER-4589
479-
MONGOC_ERROR ("timeout_msec value %" PRIu64
479+
MONGOC_ERROR ("timeout_msec value %" PRId64
480480
" exceeds supported 32-bit range",
481481
tls->timeout_msec);
482482
return -1;
@@ -511,7 +511,7 @@ mongoc_secure_channel_write (mongoc_stream_tls_t *tls,
511511

512512
if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, tls->timeout_msec))) {
513513
// CDRIVER-4589
514-
MONGOC_ERROR ("timeout_msec value %" PRIu64
514+
MONGOC_ERROR ("timeout_msec value %" PRId64
515515
" exceeds supported 32-bit range",
516516
tls->timeout_msec);
517517
return -1;

0 commit comments

Comments
 (0)