Skip to content

Commit c9e0679

Browse files
alcaeuskevinAlbs
authored andcommitted
CDRIVER-4502: Handle int64 connectionId values in hello responses (#1121)
1 parent 8955e9d commit c9e0679

7 files changed

+71
-17
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct _mongoc_apm_command_started_t {
5656
const mongoc_host_list_t *host;
5757
uint32_t server_id;
5858
bson_oid_t service_id;
59-
int32_t server_connection_id;
59+
int64_t server_connection_id;
6060
void *context;
6161
};
6262

@@ -70,7 +70,7 @@ struct _mongoc_apm_command_succeeded_t {
7070
const mongoc_host_list_t *host;
7171
uint32_t server_id;
7272
bson_oid_t service_id;
73-
int32_t server_connection_id;
73+
int64_t server_connection_id;
7474
void *context;
7575
};
7676

@@ -85,7 +85,7 @@ struct _mongoc_apm_command_failed_t {
8585
const mongoc_host_list_t *host;
8686
uint32_t server_id;
8787
bson_oid_t service_id;
88-
int32_t server_connection_id;
88+
int64_t server_connection_id;
8989
void *context;
9090
};
9191

@@ -162,7 +162,7 @@ mongoc_apm_command_started_init (mongoc_apm_command_started_t *event,
162162
const mongoc_host_list_t *host,
163163
uint32_t server_id,
164164
const bson_oid_t *service_id,
165-
int32_t server_connection_id,
165+
int64_t server_connection_id,
166166
bool *is_redacted, /* out */
167167
void *context);
168168

@@ -186,7 +186,7 @@ mongoc_apm_command_succeeded_init (mongoc_apm_command_succeeded_t *event,
186186
const mongoc_host_list_t *host,
187187
uint32_t server_id,
188188
const bson_oid_t *service_id,
189-
int32_t server_connection_id,
189+
int64_t server_connection_id,
190190
bool force_redaction,
191191
void *context);
192192

@@ -204,7 +204,7 @@ mongoc_apm_command_failed_init (mongoc_apm_command_failed_t *event,
204204
const mongoc_host_list_t *host,
205205
uint32_t server_id,
206206
const bson_oid_t *service_id,
207-
int32_t server_connection_id,
207+
int64_t server_connection_id,
208208
bool force_redaction,
209209
void *context);
210210

src/libmongoc/src/mongoc/mongoc-apm.c

+21-6
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ mongoc_apm_command_started_init (mongoc_apm_command_started_t *event,
7777
const mongoc_host_list_t *host,
7878
uint32_t server_id,
7979
const bson_oid_t *service_id,
80-
int32_t server_connection_id,
80+
int64_t server_connection_id,
8181
bool *is_redacted, /* out */
8282
void *context)
8383
{
@@ -208,7 +208,7 @@ mongoc_apm_command_succeeded_init (mongoc_apm_command_succeeded_t *event,
208208
const mongoc_host_list_t *host,
209209
uint32_t server_id,
210210
const bson_oid_t *service_id,
211-
int32_t server_connection_id,
211+
int64_t server_connection_id,
212212
bool force_redaction,
213213
void *context)
214214
{
@@ -271,7 +271,7 @@ mongoc_apm_command_failed_init (mongoc_apm_command_failed_t *event,
271271
const mongoc_host_list_t *host,
272272
uint32_t server_id,
273273
const bson_oid_t *service_id,
274-
int32_t server_connection_id,
274+
int64_t server_connection_id,
275275
bool force_redaction,
276276
void *context)
277277
{
@@ -390,7 +390,12 @@ int32_t
390390
mongoc_apm_command_started_get_server_connection_id (
391391
const mongoc_apm_command_started_t *event)
392392
{
393-
return event->server_connection_id;
393+
if (event->server_connection_id > INT32_MAX ||
394+
event->server_connection_id < INT32_MIN) {
395+
return MONGOC_NO_SERVER_CONNECTION_ID;
396+
}
397+
398+
return (int32_t) event->server_connection_id;
394399
}
395400

396401

@@ -477,7 +482,12 @@ int32_t
477482
mongoc_apm_command_succeeded_get_server_connection_id (
478483
const mongoc_apm_command_succeeded_t *event)
479484
{
480-
return event->server_connection_id;
485+
if (event->server_connection_id > INT32_MAX ||
486+
event->server_connection_id < INT32_MIN) {
487+
return MONGOC_NO_SERVER_CONNECTION_ID;
488+
}
489+
490+
return (int32_t) event->server_connection_id;
481491
}
482492

483493

@@ -568,7 +578,12 @@ int32_t
568578
mongoc_apm_command_failed_get_server_connection_id (
569579
const mongoc_apm_command_failed_t *event)
570580
{
571-
return event->server_connection_id;
581+
if (event->server_connection_id > INT32_MAX ||
582+
event->server_connection_id < INT32_MIN) {
583+
return MONGOC_NO_SERVER_CONNECTION_ID;
584+
}
585+
586+
return (int32_t) event->server_connection_id;
572587
}
573588

574589

src/libmongoc/src/mongoc/mongoc-server-description-private.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ struct _mongoc_server_description_t {
125125
* service IDs. The only server generation is mapped from kZeroServiceID */
126126
mongoc_generation_map_t *_generation_map_;
127127
bson_oid_t service_id;
128-
int32_t server_connection_id;
128+
int64_t server_connection_id;
129129
};
130130

131131
/** Get a mutable pointer to the server's generation map */

src/libmongoc/src/mongoc/mongoc-server-description.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,9 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
740740
goto failure;
741741
bson_oid_copy_unsafe (bson_iter_oid (&iter), &sd->service_id);
742742
} else if (strcmp ("connectionId", bson_iter_key (&iter)) == 0) {
743-
if (!BSON_ITER_HOLDS_INT32 (&iter))
743+
if (!BSON_ITER_HOLDS_INT (&iter))
744744
goto failure;
745-
sd->server_connection_id = bson_iter_int32 (&iter);
745+
sd->server_connection_id = bson_iter_as_int64 (&iter);
746746
}
747747
}
748748

src/libmongoc/tests/test-mongoc-server-description.c

+39
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,42 @@ test_server_description_legacy_hello_ok (void)
381381
bson_destroy (&hello_response);
382382
}
383383

384+
static void
385+
test_server_description_connection_id (void)
386+
{
387+
mongoc_server_description_t sd;
388+
bson_t *hello;
389+
bson_error_t error;
390+
391+
mongoc_server_description_init (&sd, "host:1234", 1);
392+
hello = BCON_NEW ("minWireVersion",
393+
BCON_INT32 (WIRE_VERSION_MIN),
394+
"maxWireVersion",
395+
BCON_INT32 (WIRE_VERSION_MAX),
396+
"connectionId",
397+
BCON_INT32 (1));
398+
memset (&error, 0, sizeof (bson_error_t));
399+
mongoc_server_description_handle_hello (&sd, hello, 0 /* rtt */, &error);
400+
BSON_ASSERT (sd.type == MONGOC_SERVER_STANDALONE);
401+
BSON_ASSERT (sd.server_connection_id == 1);
402+
403+
mongoc_server_description_reset (&sd);
404+
bson_destroy (hello);
405+
hello = BCON_NEW ("minWireVersion",
406+
BCON_INT32 (WIRE_VERSION_MIN),
407+
"maxWireVersion",
408+
BCON_INT32 (WIRE_VERSION_MAX),
409+
"connectionId",
410+
BCON_INT64 (1));
411+
memset (&error, 0, sizeof (bson_error_t));
412+
mongoc_server_description_handle_hello (&sd, hello, 0 /* rtt */, &error);
413+
BSON_ASSERT (sd.type == MONGOC_SERVER_STANDALONE);
414+
BSON_ASSERT (sd.server_connection_id == 1);
415+
416+
bson_destroy (hello);
417+
mongoc_server_description_cleanup (&sd);
418+
}
419+
384420
void
385421
test_server_description_install (TestSuite *suite)
386422
{
@@ -403,4 +439,7 @@ test_server_description_install (TestSuite *suite)
403439
TestSuite_Add (suite,
404440
"/server_description/legacy_hello_ok",
405441
test_server_description_legacy_hello_ok);
442+
TestSuite_Add (suite,
443+
"/server_description/connection_id",
444+
test_server_description_connection_id);
406445
}

src/libmongoc/tests/unified/entity-map.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef struct _event_t {
3030
bson_t *command;
3131
bson_t *reply;
3232
bson_oid_t service_id;
33-
int32_t server_connection_id;
33+
int64_t server_connection_id;
3434
struct _event_t *next;
3535
} event_t;
3636

src/libmongoc/tests/unified/runner.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ test_check_event (test_t *test,
11451145
}
11461146

11471147
if (!*expected_has_server_connection_id && has_server_connection_id) {
1148-
test_error ("expected no server connectionId, but got %d",
1148+
test_error ("expected no server connectionId, but got %" PRId64,
11491149
actual->server_connection_id);
11501150
}
11511151
}

0 commit comments

Comments
 (0)