Skip to content

CDRIVER-5515 fix assert when legacy exhaust cursor receives no documents #1562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/libmongoc/src/mongoc/mongoc-cursor-legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,14 @@ _mongoc_cursor_op_getmore (mongoc_cursor_t *cursor, mongoc_cursor_response_legac

cursor->cursor_id = mcd_rpc_op_reply_get_cursor_id (response->rpc);

response->reader = bson_reader_new_from_data (mcd_rpc_op_reply_get_documents (response->rpc),
mcd_rpc_op_reply_get_documents_len (response->rpc));
const void *documents = mcd_rpc_op_reply_get_documents (response->rpc);
if (documents == NULL) {
// Use a non-NULL pointer to satisfy precondition of
// `bson_reader_new_from_data`:
documents = "";
}

response->reader = bson_reader_new_from_data (documents, mcd_rpc_op_reply_get_documents_len (response->rpc));

_mongoc_cursor_monitor_succeeded (cursor,
response,
Expand Down Expand Up @@ -584,8 +590,14 @@ _mongoc_cursor_op_query_find (mongoc_cursor_t *cursor, bson_t *filter, mongoc_cu

cursor->cursor_id = mcd_rpc_op_reply_get_cursor_id (response->rpc);

response->reader = bson_reader_new_from_data (mcd_rpc_op_reply_get_documents (response->rpc),
mcd_rpc_op_reply_get_documents_len (response->rpc));
const void *documents = mcd_rpc_op_reply_get_documents (response->rpc);
if (documents == NULL) {
// Use a non-NULL pointer to satisfy precondition of
// `bson_reader_new_from_data`:
documents = "";
}

response->reader = bson_reader_new_from_data (documents, mcd_rpc_op_reply_get_documents_len (response->rpc));

if (_mongoc_cursor_get_opt_bool (cursor, MONGOC_CURSOR_EXHAUST)) {
cursor->in_exhaust = true;
Expand Down
41 changes: 41 additions & 0 deletions src/libmongoc/tests/test-mongoc-exhaust.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,45 @@ test_exhaust_cursor_works (void *context)
mongoc_client_destroy (client);
}

// `test_exhaust_cursor_no_match` is a regression test for CDRIVER-5515
static void
test_exhaust_cursor_no_match (void *context)
{
BSON_UNUSED (context);
bson_error_t error;
mongoc_client_t *client = test_framework_new_default_client ();
mongoc_collection_t *coll = mongoc_client_get_collection (client, "db", "coll");

// Drop collection to remove prior test data.
mongoc_collection_drop (coll, NULL /* ignore error */);

mongoc_cursor_t *cursor =
mongoc_collection_find_with_opts (coll, tmp_bson ("{}"), tmp_bson ("{'exhaust': true }"), NULL /* read_prefs */);
const bson_t *result;
size_t count = 0;
while (mongoc_cursor_next (cursor, &result)) {
count++;
}
// Expect an error if exhaust cursors are not supported.
const bool sharded = _mongoc_topology_get_type (cursor->client->topology) == MONGOC_TOPOLOGY_SHARDED;
int64_t wire_version;
test_framework_get_max_wire_version (&wire_version);
if (sharded && wire_version < WIRE_VERSION_MONGOS_EXHAUST) {
ASSERT (mongoc_cursor_error (cursor, &error));
ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_CURSOR, MONGOC_ERROR_CURSOR_INVALID_CURSOR, "exhaust cursors require");
} else {
// Expect no error.
ASSERT_OR_PRINT (!mongoc_cursor_error (cursor, &error), error);
// Expect no results.
ASSERT_CMPSIZE_T (count, ==, 0);
}

mongoc_cursor_destroy (cursor);
mongoc_collection_destroy (coll);
mongoc_client_destroy (client);
}


static void
test_exhaust_cursor_single (void *context)
{
Expand Down Expand Up @@ -709,6 +748,8 @@ void
test_exhaust_install (TestSuite *suite)
{
TestSuite_AddFull (suite, "/Client/exhaust_cursor/works", test_exhaust_cursor_works, NULL, NULL, skip_if_no_exhaust);
TestSuite_AddFull (
suite, "/Client/exhaust_cursor/no_match", test_exhaust_cursor_no_match, NULL, NULL, skip_if_no_exhaust);
TestSuite_AddFull (
suite, "/Client/exhaust_cursor/single", test_exhaust_cursor_single, NULL, NULL, skip_if_no_exhaust);
TestSuite_AddFull (suite, "/Client/exhaust_cursor/pool", test_exhaust_cursor_pool, NULL, NULL, skip_if_no_exhaust);
Expand Down