Skip to content

Commit 86951fd

Browse files
committed
Merge branch 'CDRIVER-2202-big-endian' into r1.7
* CDRIVER-2202-big-endian: Fix segfault when tracing and tags don't match CDRIVER-2202: Fix Big Endian support for OP_COMPRESSED
2 parents 8fed501 + 6203497 commit 86951fd

File tree

6 files changed

+140
-81
lines changed

6 files changed

+140
-81
lines changed

src/mongoc/mongoc-async-cmd.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,12 @@ _mongoc_async_cmd_phase_recv_rpc (mongoc_async_cmd_t *acmd)
355355
"Invalid reply from server.");
356356
return MONGOC_ASYNC_CMD_ERROR;
357357
}
358-
if (acmd->rpc.header.opcode == MONGOC_OPCODE_COMPRESSED) {
358+
if (BSON_UINT32_FROM_LE (acmd->rpc.header.opcode) ==
359+
MONGOC_OPCODE_COMPRESSED) {
359360
uint8_t *buf = NULL;
360-
size_t len = acmd->rpc.compressed.uncompressed_size +
361-
sizeof (mongoc_rpc_header_t);
361+
size_t len =
362+
BSON_UINT32_FROM_LE (acmd->rpc.compressed.uncompressed_size) +
363+
sizeof (mongoc_rpc_header_t);
362364

363365
buf = bson_malloc0 (len);
364366
if (!_mongoc_rpc_decompress (&acmd->rpc, buf, len)) {

src/mongoc/mongoc-cluster.c

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,11 @@ mongoc_cluster_run_command_internal (mongoc_cluster_t *cluster,
451451
}
452452
doc_len = (size_t) msg_len - reply_header_size;
453453

454-
_mongoc_rpc_swab_from_le (&rpc);
455-
if (rpc.header.opcode == MONGOC_OPCODE_COMPRESSED) {
454+
if (BSON_UINT32_FROM_LE (rpc.header.opcode) == MONGOC_OPCODE_COMPRESSED) {
456455
bson_t tmp = BSON_INITIALIZER;
457456
uint8_t *buf = NULL;
458-
size_t len =
459-
rpc.compressed.uncompressed_size + sizeof (mongoc_rpc_header_t);
457+
size_t len = BSON_UINT32_FROM_LE (rpc.compressed.uncompressed_size) +
458+
sizeof (mongoc_rpc_header_t);
460459

461460
reply_buf = bson_malloc0 (msg_len);
462461
memcpy (reply_buf, reply_header_buf, reply_header_size);
@@ -474,7 +473,6 @@ mongoc_cluster_run_command_internal (mongoc_cluster_t *cluster,
474473
if (!_mongoc_rpc_scatter (&rpc, reply_buf, msg_len)) {
475474
GOTO (done);
476475
}
477-
_mongoc_cluster_inc_ingress_rpc (&rpc);
478476

479477
buf = bson_malloc0 (len);
480478
if (!_mongoc_rpc_decompress (&rpc, buf, len)) {
@@ -486,12 +484,15 @@ mongoc_cluster_run_command_internal (mongoc_cluster_t *cluster,
486484
GOTO (done);
487485
}
488486

487+
_mongoc_rpc_swab_from_le (&rpc);
488+
_mongoc_cluster_inc_ingress_rpc (&rpc);
489+
489490
_mongoc_rpc_get_first_document (&rpc, &tmp);
490491
bson_copy_to (&tmp, reply_ptr);
491492
bson_free (reply_buf);
492493
bson_free (buf);
493-
} else if (rpc.header.opcode == MONGOC_OPCODE_REPLY &&
494-
rpc.reply_header.n_returned == 1) {
494+
} else if (BSON_UINT32_FROM_LE (rpc.header.opcode) == MONGOC_OPCODE_REPLY &&
495+
BSON_UINT32_FROM_LE (rpc.reply_header.n_returned) == 1) {
495496
reply_buf = bson_reserve_buffer (reply_ptr, (uint32_t) doc_len);
496497
BSON_ASSERT (reply_buf);
497498

@@ -505,6 +506,7 @@ mongoc_cluster_run_command_internal (mongoc_cluster_t *cluster,
505506
"socket error or timeout");
506507
GOTO (done);
507508
}
509+
_mongoc_rpc_swab_from_le (&rpc);
508510
} else {
509511
GOTO (done);
510512
}
@@ -1981,8 +1983,8 @@ _mongoc_cluster_stream_for_optype (mongoc_cluster_t *cluster,
19811983

19821984
if (!mongoc_cluster_check_interval (cluster, server_id)) {
19831985
/* Server Selection Spec: try once more */
1984-
server_id = mongoc_topology_select_server_id (
1985-
topology, optype, read_prefs, error);
1986+
server_id =
1987+
mongoc_topology_select_server_id (topology, optype, read_prefs, error);
19861988

19871989
if (!server_id) {
19881990
RETURN (NULL);
@@ -2332,6 +2334,7 @@ mongoc_cluster_sendv_to_server (mongoc_cluster_t *cluster,
23322334
need_gle = _mongoc_rpc_needs_gle (rpc, write_concern);
23332335
_mongoc_cluster_inc_egress_rpc (rpc);
23342336
_mongoc_rpc_gather (rpc, &cluster->iov);
2337+
_mongoc_rpc_swab_to_le (rpc);
23352338

23362339
#ifdef MONGOC_ENABLE_COMPRESSION
23372340
if (compressor_id) {
@@ -2344,13 +2347,13 @@ mongoc_cluster_sendv_to_server (mongoc_cluster_t *cluster,
23442347

23452348
max_msg_size = mongoc_server_stream_max_msg_size (server_stream);
23462349

2347-
if (rpc->header.msg_len > max_msg_size) {
2350+
if (BSON_UINT32_FROM_LE (rpc->header.msg_len) > max_msg_size) {
23482351
bson_set_error (error,
23492352
MONGOC_ERROR_CLIENT,
23502353
MONGOC_ERROR_CLIENT_TOO_BIG,
23512354
"Attempted to send an RPC larger than the "
23522355
"max allowed message size. Was %u, allowed %u.",
2353-
rpc->header.msg_len,
2356+
BSON_UINT32_FROM_LE (rpc->header.msg_len),
23542357
max_msg_size);
23552358
GOTO (done);
23562359
}
@@ -2362,7 +2365,7 @@ mongoc_cluster_sendv_to_server (mongoc_cluster_t *cluster,
23622365
gle.header.opcode = MONGOC_OPCODE_QUERY;
23632366
gle.query.flags = MONGOC_QUERY_NONE;
23642367

2365-
switch (rpc->header.opcode) {
2368+
switch (BSON_UINT32_FROM_LE (rpc->header.opcode)) {
23662369
case MONGOC_OPCODE_INSERT:
23672370
DB_AND_CMD_FROM_COLLECTION (cmdname, rpc->insert.collection);
23682371
break;
@@ -2389,8 +2392,6 @@ mongoc_cluster_sendv_to_server (mongoc_cluster_t *cluster,
23892392
_mongoc_rpc_swab_to_le (&gle);
23902393
}
23912394

2392-
_mongoc_rpc_swab_to_le (rpc);
2393-
23942395
if (!_mongoc_stream_writev_full (server_stream->stream,
23952396
cluster->iov.data,
23962397
cluster->iov.len,
@@ -2523,6 +2524,24 @@ mongoc_cluster_try_recv (mongoc_cluster_t *cluster,
25232524
RETURN (false);
25242525
}
25252526

2527+
if (BSON_UINT32_FROM_LE (rpc->header.opcode) == MONGOC_OPCODE_COMPRESSED) {
2528+
uint8_t *buf = NULL;
2529+
size_t len = BSON_UINT32_FROM_LE (rpc->compressed.uncompressed_size) +
2530+
sizeof (mongoc_rpc_header_t);
2531+
2532+
buf = bson_malloc0 (len);
2533+
if (!_mongoc_rpc_decompress (rpc, buf, len)) {
2534+
bson_free (buf);
2535+
bson_set_error (error,
2536+
MONGOC_ERROR_PROTOCOL,
2537+
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
2538+
"Could not decompress server reply");
2539+
RETURN (false);
2540+
}
2541+
2542+
_mongoc_buffer_destroy (buffer);
2543+
_mongoc_buffer_init (buffer, buf, len, NULL, NULL);
2544+
}
25262545
_mongoc_rpc_swab_from_le (rpc);
25272546

25282547
_mongoc_cluster_inc_ingress_rpc (rpc);

src/mongoc/mongoc-cursor.c

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ _mongoc_cursor_new_with_opts (mongoc_client_t *client,
242242
bson_set_error (&cursor->error,
243243
MONGOC_ERROR_CURSOR,
244244
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
245-
"Invalid filter: %s", validate_err.message);
245+
"Invalid filter: %s",
246+
validate_err.message);
246247
GOTO (finish);
247248
}
248249

@@ -257,7 +258,8 @@ _mongoc_cursor_new_with_opts (mongoc_client_t *client,
257258
bson_set_error (&cursor->error,
258259
MONGOC_ERROR_CURSOR,
259260
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
260-
"Invalid opts: %s", validate_err.message);
261+
"Invalid opts: %s",
262+
validate_err.message);
261263
GOTO (finish);
262264
}
263265

@@ -1184,24 +1186,6 @@ _mongoc_cursor_op_query (mongoc_cursor_t *cursor,
11841186
GOTO (done);
11851187
}
11861188

1187-
if (cursor->rpc.header.opcode == MONGOC_OPCODE_COMPRESSED) {
1188-
uint8_t *buf = NULL;
1189-
size_t len = cursor->rpc.compressed.uncompressed_size +
1190-
sizeof (mongoc_rpc_header_t);
1191-
1192-
buf = bson_malloc0 (len);
1193-
if (!_mongoc_rpc_decompress (&cursor->rpc, buf, len)) {
1194-
bson_free (buf);
1195-
bson_set_error (&cursor->error,
1196-
MONGOC_ERROR_PROTOCOL,
1197-
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
1198-
"Could not decompress server reply");
1199-
GOTO (done);
1200-
}
1201-
1202-
_mongoc_buffer_destroy (&cursor->buffer);
1203-
_mongoc_buffer_init (&cursor->buffer, buf, len, NULL, NULL);
1204-
}
12051189
if (cursor->rpc.header.opcode != MONGOC_OPCODE_REPLY) {
12061190
bson_set_error (&cursor->error,
12071191
MONGOC_ERROR_PROTOCOL,
@@ -1629,24 +1613,6 @@ _mongoc_cursor_op_getmore (mongoc_cursor_t *cursor,
16291613
GOTO (fail);
16301614
}
16311615

1632-
if (cursor->rpc.header.opcode == MONGOC_OPCODE_COMPRESSED) {
1633-
uint8_t *buf = NULL;
1634-
size_t len = cursor->rpc.compressed.uncompressed_size +
1635-
sizeof (mongoc_rpc_header_t);
1636-
1637-
buf = bson_malloc0 (len);
1638-
if (!_mongoc_rpc_decompress (&cursor->rpc, buf, len)) {
1639-
bson_free (buf);
1640-
bson_set_error (&cursor->error,
1641-
MONGOC_ERROR_PROTOCOL,
1642-
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
1643-
"Could not decompress server reply");
1644-
GOTO (fail);
1645-
}
1646-
1647-
_mongoc_buffer_destroy (&cursor->buffer);
1648-
_mongoc_buffer_init (&cursor->buffer, buf, len, NULL, NULL);
1649-
}
16501616
if (cursor->rpc.header.opcode != MONGOC_OPCODE_REPLY) {
16511617
bson_set_error (&cursor->error,
16521618
MONGOC_ERROR_PROTOCOL,

src/mongoc/mongoc-rpc-private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ _mongoc_cmd_check_ok (const bson_t *doc,
149149
bson_error_t *error);
150150

151151
bool
152-
_mongoc_rpc_decompress (mongoc_rpc_t *rpc, uint8_t *buf, size_t buflen);
152+
_mongoc_rpc_decompress (mongoc_rpc_t *rpc_le, uint8_t *buf, size_t buflen);
153153

154154
char *
155155
_mongoc_rpc_compress (struct _mongoc_cluster_t *cluster,
156156
int32_t compressor_id,
157-
mongoc_rpc_t *rpc,
157+
mongoc_rpc_t *rpc_le,
158158
bson_error_t *error);
159159

160160
BSON_END_DECLS

0 commit comments

Comments
 (0)