@@ -415,6 +415,28 @@ _bson_append (bson_t *bson, /* IN */
415
415
return ok ;
416
416
}
417
417
418
+ static BSON_INLINE bool
419
+ _string_contains_null (const char * str , size_t len )
420
+ {
421
+ for (; len ; ++ str , -- len ) {
422
+ if (* str == 0 ) {
423
+ return true;
424
+ }
425
+ }
426
+ return false;
427
+ }
428
+
429
+ #define HANDLE_KEY_LENGTH (key , key_length ) \
430
+ do { \
431
+ if (key_length < 0) { \
432
+ key_length = (int) strlen (key); \
433
+ } else { \
434
+ /* Necessary to validate embedded NULL is not present in key. */ \
435
+ if (_string_contains_null (key , key_length )) { \
436
+ return false; \
437
+ } \
438
+ } \
439
+ } while (0 )
418
440
419
441
/*
420
442
*--------------------------------------------------------------------------
@@ -457,9 +479,7 @@ _bson_append_bson_begin (bson_t *bson, /* IN */
457
479
(child_type == BSON_TYPE_ARRAY ));
458
480
BSON_ASSERT (child );
459
481
460
- if (key_length < 0 ) {
461
- key_length = (int ) strlen (key );
462
- }
482
+ HANDLE_KEY_LENGTH (key , key_length );
463
483
464
484
/*
465
485
* If the parent is an inline bson_t, then we need to convert
@@ -743,9 +763,7 @@ bson_append_array (bson_t *bson, /* IN */
743
763
BSON_ASSERT (key );
744
764
BSON_ASSERT (array );
745
765
746
- if (key_length < 0 ) {
747
- key_length = (int ) strlen (key );
748
- }
766
+ HANDLE_KEY_LENGTH (key , key_length );
749
767
750
768
/*
751
769
* Let's be a bit pedantic and ensure the array has properly formatted key
@@ -818,9 +836,7 @@ bson_append_binary (bson_t *bson, /* IN */
818
836
BSON_ASSERT (bson );
819
837
BSON_ASSERT (key );
820
838
821
- if (key_length < 0 ) {
822
- key_length = (int ) strlen (key );
823
- }
839
+ HANDLE_KEY_LENGTH (key , key_length );
824
840
825
841
subtype8 = subtype ;
826
842
@@ -896,9 +912,7 @@ bson_append_bool (bson_t *bson, /* IN */
896
912
BSON_ASSERT (bson );
897
913
BSON_ASSERT (key );
898
914
899
- if (key_length < 0 ) {
900
- key_length = (int ) strlen (key );
901
- }
915
+ HANDLE_KEY_LENGTH (key , key_length );
902
916
903
917
return _bson_append (bson ,
904
918
4 ,
@@ -950,9 +964,7 @@ bson_append_code (bson_t *bson, /* IN */
950
964
BSON_ASSERT (key );
951
965
BSON_ASSERT (javascript );
952
966
953
- if (key_length < 0 ) {
954
- key_length = (int ) strlen (key );
955
- }
967
+ HANDLE_KEY_LENGTH (key , key_length );
956
968
957
969
length = (int ) strlen (javascript ) + 1 ;
958
970
length_le = BSON_UINT32_TO_LE (length );
@@ -1011,9 +1023,7 @@ bson_append_code_with_scope (bson_t *bson, /* IN */
1011
1023
return bson_append_code (bson , key , key_length , javascript );
1012
1024
}
1013
1025
1014
- if (key_length < 0 ) {
1015
- key_length = (int ) strlen (key );
1016
- }
1026
+ HANDLE_KEY_LENGTH (key , key_length );
1017
1027
1018
1028
js_length = (int ) strlen (javascript ) + 1 ;
1019
1029
js_length_le = BSON_UINT32_TO_LE (js_length );
@@ -1075,9 +1085,7 @@ bson_append_dbpointer (bson_t *bson, /* IN */
1075
1085
BSON_ASSERT (collection );
1076
1086
BSON_ASSERT (oid );
1077
1087
1078
- if (key_length < 0 ) {
1079
- key_length = (int ) strlen (key );
1080
- }
1088
+ HANDLE_KEY_LENGTH (key , key_length );
1081
1089
1082
1090
length = (int ) strlen (collection ) + 1 ;
1083
1091
length_le = BSON_UINT32_TO_LE (length );
@@ -1134,9 +1142,7 @@ bson_append_document (bson_t *bson, /* IN */
1134
1142
BSON_ASSERT (key );
1135
1143
BSON_ASSERT (value );
1136
1144
1137
- if (key_length < 0 ) {
1138
- key_length = (int ) strlen (key );
1139
- }
1145
+ HANDLE_KEY_LENGTH (key , key_length );
1140
1146
1141
1147
return _bson_append (bson ,
1142
1148
4 ,
@@ -1160,9 +1166,7 @@ bson_append_double (bson_t *bson, const char *key, int key_length, double value)
1160
1166
BSON_ASSERT (bson );
1161
1167
BSON_ASSERT (key );
1162
1168
1163
- if (key_length < 0 ) {
1164
- key_length = (int ) strlen (key );
1165
- }
1169
+ HANDLE_KEY_LENGTH (key , key_length );
1166
1170
1167
1171
#if BSON_BYTE_ORDER == BSON_BIG_ENDIAN
1168
1172
value = BSON_DOUBLE_TO_LE (value );
@@ -1191,9 +1195,7 @@ bson_append_int32 (bson_t *bson, const char *key, int key_length, int32_t value)
1191
1195
BSON_ASSERT (bson );
1192
1196
BSON_ASSERT (key );
1193
1197
1194
- if (key_length < 0 ) {
1195
- key_length = (int ) strlen (key );
1196
- }
1198
+ HANDLE_KEY_LENGTH (key , key_length );
1197
1199
1198
1200
value_le = BSON_UINT32_TO_LE (value );
1199
1201
@@ -1220,9 +1222,7 @@ bson_append_int64 (bson_t *bson, const char *key, int key_length, int64_t value)
1220
1222
BSON_ASSERT (bson );
1221
1223
BSON_ASSERT (key );
1222
1224
1223
- if (key_length < 0 ) {
1224
- key_length = (int ) strlen (key );
1225
- }
1225
+ HANDLE_KEY_LENGTH (key , key_length );
1226
1226
1227
1227
value_le = BSON_UINT64_TO_LE (value );
1228
1228
@@ -1253,9 +1253,7 @@ bson_append_decimal128 (bson_t *bson,
1253
1253
BSON_ASSERT (key );
1254
1254
BSON_ASSERT (value );
1255
1255
1256
- if (key_length < 0 ) {
1257
- key_length = (int ) strlen (key );
1258
- }
1256
+ HANDLE_KEY_LENGTH (key , key_length );
1259
1257
1260
1258
value_le [0 ] = BSON_UINT64_TO_LE (value -> low );
1261
1259
value_le [1 ] = BSON_UINT64_TO_LE (value -> high );
@@ -1439,9 +1437,7 @@ bson_append_maxkey (bson_t *bson, const char *key, int key_length)
1439
1437
BSON_ASSERT (bson );
1440
1438
BSON_ASSERT (key );
1441
1439
1442
- if (key_length < 0 ) {
1443
- key_length = (int ) strlen (key );
1444
- }
1440
+ HANDLE_KEY_LENGTH (key , key_length );
1445
1441
1446
1442
return _bson_append (
1447
1443
bson , 3 , (1 + key_length + 1 ), 1 , & type , key_length , key , 1 , & gZero );
@@ -1456,9 +1452,7 @@ bson_append_minkey (bson_t *bson, const char *key, int key_length)
1456
1452
BSON_ASSERT (bson );
1457
1453
BSON_ASSERT (key );
1458
1454
1459
- if (key_length < 0 ) {
1460
- key_length = (int ) strlen (key );
1461
- }
1455
+ HANDLE_KEY_LENGTH (key , key_length );
1462
1456
1463
1457
return _bson_append (
1464
1458
bson , 3 , (1 + key_length + 1 ), 1 , & type , key_length , key , 1 , & gZero );
@@ -1473,9 +1467,7 @@ bson_append_null (bson_t *bson, const char *key, int key_length)
1473
1467
BSON_ASSERT (bson );
1474
1468
BSON_ASSERT (key );
1475
1469
1476
- if (key_length < 0 ) {
1477
- key_length = (int ) strlen (key );
1478
- }
1470
+ HANDLE_KEY_LENGTH (key , key_length );
1479
1471
1480
1472
return _bson_append (
1481
1473
bson , 3 , (1 + key_length + 1 ), 1 , & type , key_length , key , 1 , & gZero );
@@ -1494,9 +1486,7 @@ bson_append_oid (bson_t *bson,
1494
1486
BSON_ASSERT (key );
1495
1487
BSON_ASSERT (value );
1496
1488
1497
- if (key_length < 0 ) {
1498
- key_length = (int ) strlen (key );
1499
- }
1489
+ HANDLE_KEY_LENGTH (key , key_length );
1500
1490
1501
1491
return _bson_append (bson ,
1502
1492
4 ,
@@ -1573,12 +1563,15 @@ bson_append_regex_w_len (bson_t *bson,
1573
1563
BSON_ASSERT (bson );
1574
1564
BSON_ASSERT (key );
1575
1565
1576
- if (key_length < 0 ) {
1577
- key_length = (int ) strlen (key );
1578
- }
1566
+ HANDLE_KEY_LENGTH (key , key_length );
1579
1567
1580
1568
if (regex_length < 0 ) {
1581
1569
regex_length = (int ) strlen (regex );
1570
+ } else {
1571
+ /* Necessary to validate embedded NULL is not present in key. */
1572
+ if (_string_contains_null (regex , regex_length )) {
1573
+ return false;
1574
+ }
1582
1575
}
1583
1576
1584
1577
if (!regex ) {
@@ -1630,9 +1623,7 @@ bson_append_utf8 (
1630
1623
return bson_append_null (bson , key , key_length );
1631
1624
}
1632
1625
1633
- if (BSON_UNLIKELY (key_length < 0 )) {
1634
- key_length = (int ) strlen (key );
1635
- }
1626
+ HANDLE_KEY_LENGTH (key , key_length );
1636
1627
1637
1628
if (BSON_UNLIKELY (length < 0 )) {
1638
1629
length = (int ) strlen (value );
@@ -1672,9 +1663,7 @@ bson_append_symbol (
1672
1663
return bson_append_null (bson , key , key_length );
1673
1664
}
1674
1665
1675
- if (key_length < 0 ) {
1676
- key_length = (int ) strlen (key );
1677
- }
1666
+ HANDLE_KEY_LENGTH (key , key_length );
1678
1667
1679
1668
if (length < 0 ) {
1680
1669
length = (int ) strlen (value );
@@ -1729,9 +1718,7 @@ bson_append_timestamp (bson_t *bson,
1729
1718
BSON_ASSERT (bson );
1730
1719
BSON_ASSERT (key );
1731
1720
1732
- if (key_length < 0 ) {
1733
- key_length = (int ) strlen (key );
1734
- }
1721
+ HANDLE_KEY_LENGTH (key , key_length );
1735
1722
1736
1723
value = ((((uint64_t ) timestamp ) << 32 ) | ((uint64_t ) increment ));
1737
1724
value = BSON_UINT64_TO_LE (value );
@@ -1773,9 +1760,7 @@ bson_append_date_time (bson_t *bson,
1773
1760
BSON_ASSERT (bson );
1774
1761
BSON_ASSERT (key );
1775
1762
1776
- if (key_length < 0 ) {
1777
- key_length = (int ) strlen (key );
1778
- }
1763
+ HANDLE_KEY_LENGTH (key , key_length );
1779
1764
1780
1765
value_le = BSON_UINT64_TO_LE (value );
1781
1766
@@ -1819,9 +1804,7 @@ bson_append_undefined (bson_t *bson, const char *key, int key_length)
1819
1804
BSON_ASSERT (bson );
1820
1805
BSON_ASSERT (key );
1821
1806
1822
- if (key_length < 0 ) {
1823
- key_length = (int ) strlen (key );
1824
- }
1807
+ HANDLE_KEY_LENGTH (key , key_length );
1825
1808
1826
1809
return _bson_append (
1827
1810
bson , 3 , (1 + key_length + 1 ), 1 , & type , key_length , key , 1 , & gZero );
0 commit comments