Skip to content

Commit 751c853

Browse files
xdgajdavis
authored andcommitted
CDRIVER-1921 cursor from static reply
1 parent a5bffaf commit 751c853

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/mongoc/mongoc-cursor-cursorid.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ _mongoc_cursor_cursorid_init_with_reply (mongoc_cursor_t *cursor,
373373
BSON_ASSERT (cid);
374374

375375
bson_destroy (&cid->array);
376-
bson_steal (&cid->array, reply);
376+
if (!bson_steal (&cid->array, reply)) {
377+
bson_steal (&cid->array, bson_copy (reply));
378+
}
377379

378380
if (!_mongoc_cursor_cursorid_start_batch (cursor)) {
379381
bson_set_error (&cursor->error,

tests/test-mongoc-cursor.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,44 @@ test_cursor_new_invalid (void)
734734
mongoc_client_destroy (client);
735735
}
736736

737+
static void
738+
test_cursor_new_static (void)
739+
{
740+
mongoc_client_t *client;
741+
bson_error_t error;
742+
mongoc_cursor_t *cursor;
743+
bson_t *bson_alloced;
744+
bson_t bson_static;
745+
746+
bson_alloced = tmp_bson ("{ 'ok':1,"
747+
" 'cursor': {"
748+
" 'id': 0,"
749+
" 'ns': 'test.foo',"
750+
" 'firstBatch': [{'x': 1}, {'x': 2}]}}");
751+
752+
ASSERT (bson_init_static (&bson_static,
753+
bson_get_data (bson_alloced),
754+
bson_alloced->len));
755+
756+
/* test heap-allocated bson */
757+
client = test_framework_client_new ();
758+
cursor = mongoc_cursor_new_from_command_reply (client,
759+
bson_copy (bson_alloced),
760+
0);
761+
762+
ASSERT (cursor);
763+
ASSERT (!mongoc_cursor_error (cursor, &error));
764+
mongoc_cursor_destroy (cursor);
765+
766+
/* test static bson */
767+
cursor = mongoc_cursor_new_from_command_reply (client, &bson_static, 0);
768+
ASSERT (cursor);
769+
ASSERT (!mongoc_cursor_error (cursor, &error));
770+
771+
mongoc_cursor_destroy (cursor);
772+
mongoc_client_destroy (client);
773+
}
774+
737775

738776
static void
739777
test_cursor_hint_errors (void)
@@ -1516,6 +1554,7 @@ test_cursor_install (TestSuite *suite)
15161554
test_cursor_new_from_find_batches, NULL, NULL,
15171555
test_framework_skip_if_max_wire_version_less_than_4);
15181556
TestSuite_AddLive (suite, "/Cursor/new_invalid", test_cursor_new_invalid);
1557+
TestSuite_AddLive (suite, "/Cursor/new_static", test_cursor_new_static);
15191558
TestSuite_AddLive (suite, "/Cursor/hint/errors", test_cursor_hint_errors);
15201559
TestSuite_Add (suite, "/Cursor/hint/single/secondary", test_hint_single_secondary);
15211560
TestSuite_Add (suite, "/Cursor/hint/single/primary", test_hint_single_primary);

0 commit comments

Comments
 (0)