@@ -583,6 +583,46 @@ _mongoc_monitor_legacy_write_succeeded (mongoc_client_t *client,
583
583
}
584
584
585
585
586
+ /*
587
+ *-------------------------------------------------------------------------
588
+ *
589
+ * too_large_error --
590
+ *
591
+ * Fill a bson_error_t and optional bson_t with error info after
592
+ * receiving a document for bulk insert, update, or remove that is
593
+ * larger than max_bson_size.
594
+ *
595
+ * "err_doc" should be NULL or an empty initialized bson_t.
596
+ *
597
+ * Returns:
598
+ * None.
599
+ *
600
+ * Side effects:
601
+ * "error" and optionally "err_doc" are filled out.
602
+ *
603
+ *-------------------------------------------------------------------------
604
+ */
605
+
606
+ static void
607
+ too_large_error (bson_error_t * error ,
608
+ int32_t idx ,
609
+ int32_t len ,
610
+ int32_t max_bson_size ,
611
+ bson_t * err_doc )
612
+ {
613
+ bson_set_error (error , MONGOC_ERROR_BSON , MONGOC_ERROR_BSON_INVALID ,
614
+ "Document %u is too large for the cluster. "
615
+ "Document is %u bytes, max is %d." ,
616
+ idx , len , max_bson_size );
617
+
618
+ if (err_doc ) {
619
+ BSON_APPEND_INT32 (err_doc , "index" , idx );
620
+ BSON_APPEND_UTF8 (err_doc , "err" , error -> message );
621
+ BSON_APPEND_INT32 (err_doc , "code" , MONGOC_ERROR_BSON_INVALID );
622
+ }
623
+ }
624
+
625
+
586
626
static void
587
627
_mongoc_write_command_delete_legacy (mongoc_write_command_t * command ,
588
628
mongoc_client_t * client ,
@@ -595,6 +635,7 @@ _mongoc_write_command_delete_legacy (mongoc_write_command_t *command,
595
635
bson_error_t * error )
596
636
{
597
637
int64_t started ;
638
+ int32_t max_bson_obj_size ;
598
639
const uint8_t * data ;
599
640
mongoc_rpc_t rpc ;
600
641
uint32_t request_id ;
@@ -616,6 +657,8 @@ _mongoc_write_command_delete_legacy (mongoc_write_command_t *command,
616
657
617
658
started = bson_get_monotonic_time ();
618
659
660
+ max_bson_obj_size = mongoc_server_stream_max_bson_obj_size (server_stream );
661
+
619
662
r = bson_iter_init (& iter , command -> documents );
620
663
if (!r ) {
621
664
BSON_ASSERT (false);
@@ -647,6 +690,11 @@ _mongoc_write_command_delete_legacy (mongoc_write_command_t *command,
647
690
bson_iter_document (& q_iter , & len , & data );
648
691
BSON_ASSERT (data );
649
692
BSON_ASSERT (len >= 5 );
693
+ if (len > max_bson_obj_size ) {
694
+ too_large_error (error , 0 , len , max_bson_obj_size , NULL );
695
+ result -> failed = true;
696
+ EXIT ;
697
+ }
650
698
651
699
request_id = ++ client -> cluster .request_id ;
652
700
@@ -709,49 +757,6 @@ _mongoc_write_command_delete_legacy (mongoc_write_command_t *command,
709
757
}
710
758
711
759
712
- /*
713
- *-------------------------------------------------------------------------
714
- *
715
- * too_large_error --
716
- *
717
- * Fill a bson_error_t and optional bson_t with error info after
718
- * receiving a document for bulk insert, update, or remove that is
719
- * larger than max_bson_size.
720
- *
721
- * "err_doc" should be NULL or an empty initialized bson_t.
722
- *
723
- * Returns:
724
- * None.
725
- *
726
- * Side effects:
727
- * "error" and optionally "err_doc" are filled out.
728
- *
729
- *-------------------------------------------------------------------------
730
- */
731
-
732
- static void
733
- too_large_error (bson_error_t * error ,
734
- int32_t idx ,
735
- int32_t len ,
736
- int32_t max_bson_size ,
737
- bson_t * err_doc )
738
- {
739
- /* MongoDB 2.6 uses code 2 for "too large". TODO: see CDRIVER-644 */
740
- const int code = 2 ;
741
-
742
- bson_set_error (error , MONGOC_ERROR_BSON , code ,
743
- "Document %u is too large for the cluster. "
744
- "Document is %u bytes, max is %d." ,
745
- idx , len , max_bson_size );
746
-
747
- if (err_doc ) {
748
- BSON_APPEND_INT32 (err_doc , "index" , idx );
749
- BSON_APPEND_UTF8 (err_doc , "err" , error -> message );
750
- BSON_APPEND_INT32 (err_doc , "code" , code );
751
- }
752
- }
753
-
754
-
755
760
static void
756
761
_mongoc_write_command_insert_legacy (mongoc_write_command_t * command ,
757
762
mongoc_client_t * client ,
@@ -1007,6 +1012,7 @@ _mongoc_write_command_update_legacy (mongoc_write_command_t *command,
1007
1012
bson_error_t * error )
1008
1013
{
1009
1014
int64_t started ;
1015
+ int32_t max_bson_obj_size ;
1010
1016
mongoc_rpc_t rpc ;
1011
1017
uint32_t request_id = 0 ;
1012
1018
bson_iter_t iter , subiter , subsubiter ;
@@ -1033,6 +1039,8 @@ _mongoc_write_command_update_legacy (mongoc_write_command_t *command,
1033
1039
1034
1040
started = bson_get_monotonic_time ();
1035
1041
1042
+ max_bson_obj_size = mongoc_server_stream_max_bson_obj_size (server_stream );
1043
+
1036
1044
bson_iter_init (& iter , command -> documents );
1037
1045
while (bson_iter_next (& iter )) {
1038
1046
if (bson_iter_recurse (& iter , & subiter ) &&
@@ -1085,11 +1093,23 @@ _mongoc_write_command_update_legacy (mongoc_write_command_t *command,
1085
1093
while (bson_iter_next (& subiter )) {
1086
1094
if (strcmp (bson_iter_key (& subiter ), "u" ) == 0 ) {
1087
1095
bson_iter_document (& subiter , & len , & data );
1096
+ if (len > max_bson_obj_size ) {
1097
+ too_large_error (error , 0 , len , max_bson_obj_size , NULL );
1098
+ result -> failed = true;
1099
+ EXIT ;
1100
+ }
1101
+
1088
1102
rpc .update .update = data ;
1089
1103
bson_init_static (& update , data , len );
1090
1104
has_update = true;
1091
1105
} else if (strcmp (bson_iter_key (& subiter ), "q" ) == 0 ) {
1092
1106
bson_iter_document (& subiter , & len , & data );
1107
+ if (len > max_bson_obj_size ) {
1108
+ too_large_error (error , 0 , len , max_bson_obj_size , NULL );
1109
+ result -> failed = true;
1110
+ EXIT ;
1111
+ }
1112
+
1093
1113
rpc .update .selector = data ;
1094
1114
bson_init_static (& selector , data , len );
1095
1115
has_selector = true;
@@ -1533,7 +1553,9 @@ _append_write_err_legacy (mongoc_write_result_t *result,
1533
1553
1534
1554
BSON_ASSERT (code > 0 );
1535
1555
1536
- bson_set_error (& result -> error , domain , (uint32_t ) code , "%s" , err );
1556
+ if (!result -> error .domain ) {
1557
+ bson_set_error (& result -> error , domain , (uint32_t ) code , "%s" , err );
1558
+ }
1537
1559
1538
1560
/* stop processing, if result->ordered */
1539
1561
result -> failed = true;
@@ -1927,21 +1949,28 @@ _set_error_from_response (bson_t *bson_array,
1927
1949
1928
1950
1929
1951
bool
1930
- _mongoc_write_result_complete (mongoc_write_result_t * result , /* IN */
1931
- int32_t error_api_version , /* IN */
1932
- const mongoc_write_concern_t * wc , /* IN */
1933
- bson_t * bson , /* OUT */
1934
- bson_error_t * error ) /* OUT */
1952
+ _mongoc_write_result_complete (mongoc_write_result_t * result , /* IN */
1953
+ int32_t error_api_version , /* IN */
1954
+ const mongoc_write_concern_t * wc , /* IN */
1955
+ mongoc_error_domain_t err_domain_override , /* IN */
1956
+ bson_t * bson , /* OUT */
1957
+ bson_error_t * error ) /* OUT */
1935
1958
{
1936
1959
mongoc_error_domain_t domain ;
1937
1960
1938
1961
ENTRY ;
1939
1962
1940
1963
BSON_ASSERT (result );
1941
1964
1942
- domain = error_api_version >= MONGOC_ERROR_API_VERSION_2
1943
- ? MONGOC_ERROR_SERVER
1944
- : MONGOC_ERROR_COMMAND ;
1965
+ if (error_api_version >= MONGOC_ERROR_API_VERSION_2 ) {
1966
+ domain = MONGOC_ERROR_SERVER ;
1967
+ } else if (err_domain_override ) {
1968
+ domain = err_domain_override ;
1969
+ } else if (result -> error .domain ) {
1970
+ domain = (mongoc_error_domain_t ) result -> error .domain ;
1971
+ } else {
1972
+ domain = MONGOC_ERROR_COLLECTION ;
1973
+ }
1945
1974
1946
1975
if (bson && mongoc_write_concern_is_acknowledged (wc )) {
1947
1976
BSON_APPEND_INT32 (bson , "nInserted" , result -> nInserted );
0 commit comments