Skip to content

Commit 471bfb0

Browse files
committed
CDRIVER-2431 check for empty command before comparing name
1 parent 4df4560 commit 471bfb0

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/mongoc/mongoc-client.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,7 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
17131713
bson_error_t *error)
17141714
{
17151715
mongoc_cmd_parts_t parts;
1716+
const char *command_name;
17161717
mongoc_server_stream_t *server_stream = NULL;
17171718
mongoc_cluster_t *cluster;
17181719
bson_t reply_local;
@@ -1730,6 +1731,16 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
17301731
parts.is_read_command = (mode & MONGOC_CMD_READ);
17311732
parts.is_write_command = (mode & MONGOC_CMD_WRITE);
17321733

1734+
command_name = _mongoc_get_command_name (command);
1735+
1736+
if (!command_name) {
1737+
bson_set_error (error,
1738+
MONGOC_ERROR_COMMAND,
1739+
MONGOC_ERROR_COMMAND_INVALID_ARG,
1740+
"Empty command document");
1741+
GOTO (err);
1742+
}
1743+
17331744
reply_ptr = reply ? reply : &reply_local;
17341745

17351746
if (mode == MONGOC_CMD_READ || mode == MONGOC_CMD_RAW) {
@@ -1783,8 +1794,7 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
17831794
if ((mode & MONGOC_CMD_WRITE) &&
17841795
!mongoc_write_concern_is_default (default_wc) &&
17851796
(!opts || !bson_has_field (opts, "writeConcern"))) {
1786-
bool is_fam =
1787-
!strcasecmp (_mongoc_get_command_name (command), "findandmodify");
1797+
bool is_fam = !strcasecmp (command_name, "findandmodify");
17881798

17891799
if ((is_fam && wire_version >= WIRE_VERSION_FAM_WRITE_CONCERN) ||
17901800
(!is_fam && wire_version >= WIRE_VERSION_CMD_WRITE_CONCERN)) {

src/mongoc/mongoc-cmd.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ mongoc_cmd_parts_append_opts (mongoc_cmd_parts_t *parts,
108108
int max_wire_version,
109109
bson_error_t *error)
110110
{
111+
const char *command_name;
111112
bool is_fam;
112113
mongoc_client_session_t *cs = NULL;
113114
mongoc_write_concern_t *wc;
@@ -120,8 +121,17 @@ mongoc_cmd_parts_append_opts (mongoc_cmd_parts_t *parts,
120121
/* not yet assembled */
121122
BSON_ASSERT (!parts->assembled.command);
122123

123-
is_fam =
124-
!strcasecmp (_mongoc_get_command_name (parts->body), "findandmodify");
124+
command_name = _mongoc_get_command_name (parts->body);
125+
126+
if (!command_name) {
127+
bson_set_error (error,
128+
MONGOC_ERROR_COMMAND,
129+
MONGOC_ERROR_COMMAND_INVALID_ARG,
130+
"Empty command document");
131+
RETURN (false);
132+
}
133+
134+
is_fam = !strcasecmp (command_name, "findandmodify");
125135

126136
while (bson_iter_next (iter)) {
127137
if (BSON_ITER_IS_KEY (iter, "collation")) {

tests/test-mongoc-client.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,7 @@ static void
15311531
test_command_empty (void)
15321532
{
15331533
mongoc_client_t *client;
1534+
mongoc_write_concern_t *wc;
15341535
bson_error_t error;
15351536
bool r;
15361537

@@ -1544,6 +1545,29 @@ test_command_empty (void)
15441545
MONGOC_ERROR_COMMAND_INVALID_ARG,
15451546
"Empty command document");
15461547

1548+
r = mongoc_client_command_with_opts (
1549+
client, "admin", tmp_bson ("{}"), NULL, tmp_bson ("{}"), NULL, &error);
1550+
1551+
ASSERT (!r);
1552+
ASSERT_ERROR_CONTAINS (error,
1553+
MONGOC_ERROR_COMMAND,
1554+
MONGOC_ERROR_COMMAND_INVALID_ARG,
1555+
"Empty command document");
1556+
1557+
wc = mongoc_write_concern_new ();
1558+
mongoc_write_concern_set_w (wc, 1);
1559+
mongoc_client_set_write_concern (client, wc);
1560+
1561+
r = mongoc_client_write_command_with_opts (
1562+
client, "admin", tmp_bson ("{}"), NULL, NULL, &error);
1563+
1564+
ASSERT (!r);
1565+
ASSERT_ERROR_CONTAINS (error,
1566+
MONGOC_ERROR_COMMAND,
1567+
MONGOC_ERROR_COMMAND_INVALID_ARG,
1568+
"Empty command document");
1569+
1570+
mongoc_write_concern_destroy (wc);
15471571
mongoc_client_destroy (client);
15481572
}
15491573

0 commit comments

Comments
 (0)