Skip to content

Commit e8c89c0

Browse files
jmikolaajdavis
authored andcommitted
CDRIVER-2072: Always initialize cursor filter and opts
mongoc_cursor_destroy() always attempts to destroy these documents, which would previously crash if they were left uninitialized when an error was reported by _mongoc_cursor_new_with_opts().
1 parent 052809b commit e8c89c0

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

src/mongoc/mongoc-cursor.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ _mongoc_cursor_new_with_opts (mongoc_client_t *client,
232232
cursor->client = client;
233233
cursor->is_command = is_command ? 1 : 0;
234234

235+
bson_init (&cursor->filter);
236+
bson_init (&cursor->opts);
237+
235238
if (filter) {
236239
if (!bson_validate (filter, BSON_VALIDATE_EMPTY_KEYS, NULL)) {
237240
MARK_FAILED (cursor);
@@ -241,9 +244,8 @@ _mongoc_cursor_new_with_opts (mongoc_client_t *client,
241244
GOTO (finish);
242245
}
243246

247+
bson_destroy (&cursor->filter);
244248
bson_copy_to (filter, &cursor->filter);
245-
} else {
246-
bson_init (&cursor->filter);
247249
}
248250

249251
if (opts) {
@@ -263,7 +265,6 @@ _mongoc_cursor_new_with_opts (mongoc_client_t *client,
263265
GOTO (finish);
264266
}
265267

266-
bson_init (&cursor->opts);
267268
bson_copy_to_excluding_noinit (opts, &cursor->opts, "serverId", NULL);
268269

269270
/* true if there's a valid serverId or no serverId, false on err */
@@ -277,8 +278,6 @@ _mongoc_cursor_new_with_opts (mongoc_client_t *client,
277278
if (server_id) {
278279
mongoc_cursor_set_hint (cursor, server_id);
279280
}
280-
} else {
281-
bson_init (&cursor->opts);
282281
}
283282

284283
cursor->read_prefs = read_prefs ?

tests/test-mongoc-cursor.c

+72
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,74 @@ test_cursor_new_invalid (void)
702702
mongoc_client_destroy (client);
703703
}
704704

705+
706+
static void
707+
test_cursor_new_invalid_filter (void)
708+
{
709+
mongoc_client_t *client;
710+
mongoc_collection_t *collection;
711+
mongoc_cursor_t *cursor;
712+
bson_error_t error;
713+
714+
client = test_framework_client_new ();
715+
collection = mongoc_client_get_collection (client, "test", "test");
716+
717+
cursor = mongoc_collection_find_with_opts (
718+
collection, tmp_bson ("{'': 1}"), NULL, NULL);
719+
720+
ASSERT (cursor);
721+
ASSERT (mongoc_cursor_error (cursor, &error));
722+
ASSERT_ERROR_CONTAINS (error,
723+
MONGOC_ERROR_CURSOR,
724+
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
725+
"Empty keys are not allowed in 'filter'.");
726+
727+
mongoc_cursor_destroy (cursor);
728+
mongoc_collection_destroy (collection);
729+
mongoc_client_destroy (client);
730+
}
731+
732+
733+
static void
734+
test_cursor_new_invalid_opts (void)
735+
{
736+
mongoc_client_t *client;
737+
mongoc_collection_t *collection;
738+
mongoc_cursor_t *cursor;
739+
bson_error_t error;
740+
741+
client = test_framework_client_new ();
742+
collection = mongoc_client_get_collection (client, "test", "test");
743+
744+
cursor = mongoc_collection_find_with_opts (
745+
collection, tmp_bson (NULL), tmp_bson ("{'projection': {'': 1}}"), NULL);
746+
747+
ASSERT (cursor);
748+
ASSERT (mongoc_cursor_error (cursor, &error));
749+
ASSERT_ERROR_CONTAINS (error,
750+
MONGOC_ERROR_CURSOR,
751+
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
752+
"Cannot use empty keys in 'opts'.");
753+
754+
mongoc_cursor_destroy (cursor);
755+
756+
cursor = mongoc_collection_find_with_opts (
757+
collection, tmp_bson (NULL), tmp_bson ("{'$invalid': 1}"), NULL);
758+
759+
ASSERT (cursor);
760+
ASSERT (mongoc_cursor_error (cursor, &error));
761+
ASSERT_ERROR_CONTAINS (error,
762+
MONGOC_ERROR_CURSOR,
763+
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
764+
"Cannot use $-modifiers in 'opts'.");
765+
766+
mongoc_cursor_destroy (cursor);
767+
768+
mongoc_collection_destroy (collection);
769+
mongoc_client_destroy (client);
770+
}
771+
772+
705773
static void
706774
test_cursor_new_static (void)
707775
{
@@ -1521,6 +1589,10 @@ test_cursor_install (TestSuite *suite)
15211589
test_cursor_new_from_find_batches, NULL, NULL,
15221590
test_framework_skip_if_max_wire_version_less_than_4);
15231591
TestSuite_AddLive (suite, "/Cursor/new_invalid", test_cursor_new_invalid);
1592+
TestSuite_AddLive (
1593+
suite, "/Cursor/new_invalid_filter", test_cursor_new_invalid_filter);
1594+
TestSuite_AddLive (
1595+
suite, "/Cursor/new_invalid_opts", test_cursor_new_invalid_opts);
15241596
TestSuite_AddLive (suite, "/Cursor/new_static", test_cursor_new_static);
15251597
TestSuite_AddLive (suite, "/Cursor/hint/errors", test_cursor_hint_errors);
15261598
TestSuite_Add (suite, "/Cursor/hint/single/secondary", test_hint_single_secondary);

0 commit comments

Comments
 (0)