Skip to content

Commit 32442ad

Browse files
committed
Merge branch 'CDRIVER-2250-null-value-in-strings'
* CDRIVER-2250-null-value-in-strings: CDRIVER-2250 Cannot insert strings with null bytes
2 parents e66b022 + cee8946 commit 32442ad

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

src/mongoc/mongoc-util.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ _mongoc_validate_legacy_index (const bson_t *doc, bson_error_t *error)
216216

217217

218218
const bson_validate_flags_t insert_vflags =
219-
(bson_validate_flags_t) BSON_VALIDATE_UTF8 | BSON_VALIDATE_EMPTY_KEYS |
220-
BSON_VALIDATE_DOT_KEYS | BSON_VALIDATE_DOLLAR_KEYS;
219+
(bson_validate_flags_t) BSON_VALIDATE_UTF8 | BSON_VALIDATE_UTF8_ALLOW_NULL |
220+
BSON_VALIDATE_EMPTY_KEYS | BSON_VALIDATE_DOT_KEYS |
221+
BSON_VALIDATE_DOLLAR_KEYS;
221222

222223
bool
223224
_mongoc_validate_new_document (const bson_t *doc, bson_error_t *error)
@@ -261,7 +262,8 @@ _mongoc_validate_update (const bson_t *update, bson_error_t *error)
261262
bson_error_t validate_err;
262263
bson_iter_t iter;
263264
const char *key;
264-
int vflags = BSON_VALIDATE_UTF8 | BSON_VALIDATE_EMPTY_KEYS;
265+
int vflags = BSON_VALIDATE_UTF8 | BSON_VALIDATE_UTF8_ALLOW_NULL |
266+
BSON_VALIDATE_EMPTY_KEYS;
265267

266268
if (!bson_validate_with_error (
267269
update, (bson_validate_flags_t) vflags, &validate_err)) {

tests/test-mongoc-collection.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,88 @@ test_insert (void)
490490
}
491491

492492

493+
static void
494+
test_insert_null (void)
495+
{
496+
mongoc_collection_t *collection;
497+
mongoc_bulk_operation_t *bulk;
498+
mongoc_client_t *client;
499+
mongoc_cursor_t *cursor;
500+
bson_error_t error;
501+
bson_t reply;
502+
const bson_t *out;
503+
bool ret;
504+
bson_t doc;
505+
bson_t filter = BSON_INITIALIZER;
506+
bson_iter_t iter;
507+
uint32_t len;
508+
509+
client = test_framework_client_new ();
510+
ASSERT (client);
511+
512+
collection =
513+
mongoc_client_get_collection (client, "test", "test_null_insert");
514+
ASSERT (collection);
515+
516+
mongoc_collection_drop (collection, &error);
517+
518+
bson_init (&doc);
519+
bson_append_utf8 (&doc, "hello", 5, "wor\0ld", 6);
520+
ret = mongoc_collection_insert (
521+
collection, MONGOC_INSERT_NONE, &doc, NULL, &error);
522+
ASSERT_OR_PRINT (ret, error);
523+
524+
bulk = mongoc_collection_create_bulk_operation (collection, true, NULL);
525+
mongoc_bulk_operation_insert (bulk, &doc);
526+
ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
527+
ASSERT_OR_PRINT (ret, error);
528+
ASSERT_MATCH (&reply,
529+
"{'nInserted': 1,"
530+
" 'nMatched': 0,"
531+
" 'nModified': 0,"
532+
" 'nRemoved': 0,"
533+
" 'nUpserted': 0,"
534+
" 'writeErrors': []}");
535+
bson_destroy (&doc);
536+
bson_destroy (&reply);
537+
538+
cursor = mongoc_collection_find_with_opts (collection, &filter, NULL, NULL);
539+
ASSERT (mongoc_cursor_next (cursor, &out));
540+
ASSERT (bson_iter_init_find (&iter, out, "hello"));
541+
ASSERT (!memcmp (bson_iter_utf8 (&iter, &len), "wor\0ld", 6));
542+
ASSERT_CMPINT (len, ==, 6);
543+
544+
ASSERT (mongoc_cursor_next (cursor, &out));
545+
ASSERT (bson_iter_init_find (&iter, out, "hello"));
546+
ASSERT (!memcmp (bson_iter_utf8 (&iter, &len), "wor\0ld", 6));
547+
ASSERT_CMPINT (len, ==, 6);
548+
549+
mongoc_cursor_destroy (cursor);
550+
mongoc_bulk_operation_destroy (bulk);
551+
552+
bulk = mongoc_collection_create_bulk_operation (collection, true, NULL);
553+
mongoc_bulk_operation_remove_one (bulk, &doc);
554+
ret = mongoc_bulk_operation_update_one_with_opts (
555+
bulk, &doc, tmp_bson ("{'$set': {'x': 1}}"), NULL, &error);
556+
ASSERT_OR_PRINT (ret, error);
557+
ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
558+
ASSERT_OR_PRINT (ret, error);
559+
ASSERT_MATCH (&reply,
560+
"{'nInserted': 0,"
561+
" 'nMatched': 1,"
562+
" 'nModified': 1,"
563+
" 'nRemoved': 1,"
564+
" 'nUpserted': 0,"
565+
" 'writeErrors': []}");
566+
567+
bson_destroy (&filter);
568+
bson_destroy (&reply);
569+
mongoc_bulk_operation_destroy (bulk);
570+
mongoc_collection_destroy (collection);
571+
mongoc_client_destroy (client);
572+
}
573+
574+
493575
static void
494576
test_insert_oversize (void *ctx)
495577
{
@@ -4899,6 +4981,8 @@ test_collection_install (TestSuite *suite)
48994981

49004982
TestSuite_AddLive (suite, "/Collection/copy", test_copy);
49014983
TestSuite_AddLive (suite, "/Collection/insert", test_insert);
4984+
TestSuite_AddLive (
4985+
suite, "/Collection/insert/null_string", test_insert_null);
49024986
TestSuite_AddFull (suite,
49034987
"/Collection/insert/oversize",
49044988
test_insert_oversize,

0 commit comments

Comments
 (0)