@@ -1788,7 +1788,6 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t* uri, bson_t* option
17881788static bool php_phongo_apply_wc_options_to_uri (mongoc_uri_t * uri , bson_t * options TSRMLS_DC ) /* {{{ */
17891789{
17901790 bson_iter_t iter ;
1791- int32_t wtimeoutms ;
17921791 mongoc_write_concern_t * new_wc ;
17931792 const mongoc_write_concern_t * old_wc ;
17941793
@@ -1811,8 +1810,6 @@ static bool php_phongo_apply_wc_options_to_uri(mongoc_uri_t* uri, bson_t* option
18111810 return true;
18121811 }
18131812
1814- wtimeoutms = mongoc_write_concern_get_wtimeout (old_wc );
1815-
18161813 new_wc = mongoc_write_concern_copy (old_wc );
18171814
18181815 if (bson_iter_init_find_case (& iter , options , MONGOC_URI_SAFE )) {
@@ -1827,14 +1824,25 @@ static bool php_phongo_apply_wc_options_to_uri(mongoc_uri_t* uri, bson_t* option
18271824 }
18281825
18291826 if (bson_iter_init_find_case (& iter , options , MONGOC_URI_WTIMEOUTMS )) {
1827+ int32_t wtimeout ;
1828+
18301829 if (!BSON_ITER_HOLDS_INT32 (& iter )) {
18311830 PHONGO_URI_INVALID_TYPE (iter , "32-bit integer" );
18321831 mongoc_write_concern_destroy (new_wc );
18331832
18341833 return false;
18351834 }
18361835
1837- wtimeoutms = bson_iter_int32 (& iter );
1836+ wtimeout = bson_iter_int32 (& iter );
1837+
1838+ if (wtimeout < 0 ) {
1839+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "Expected wtimeoutMS to be >= 0, %d given" , wtimeout );
1840+ mongoc_write_concern_destroy (new_wc );
1841+
1842+ return false;
1843+ }
1844+
1845+ mongoc_write_concern_set_wtimeout (new_wc , wtimeout );
18381846 }
18391847
18401848 if (bson_iter_init_find_case (& iter , options , MONGOC_URI_JOURNAL )) {
@@ -1872,7 +1880,8 @@ static bool php_phongo_apply_wc_options_to_uri(mongoc_uri_t* uri, bson_t* option
18721880 const char * str = bson_iter_utf8 (& iter , NULL );
18731881
18741882 if (0 == strcasecmp (PHONGO_WRITE_CONCERN_W_MAJORITY , str )) {
1875- mongoc_write_concern_set_wmajority (new_wc , wtimeoutms );
1883+ /* wtimeoutMS is set independently, so preserve its value here */
1884+ mongoc_write_concern_set_wmajority (new_wc , mongoc_write_concern_get_wtimeout (new_wc ));
18761885 } else {
18771886 mongoc_write_concern_set_wtag (new_wc , str );
18781887 }
@@ -1884,15 +1893,6 @@ static bool php_phongo_apply_wc_options_to_uri(mongoc_uri_t* uri, bson_t* option
18841893 }
18851894 }
18861895
1887- /* Only set wtimeout if it's still applicable; otherwise, clear it. */
1888- if (mongoc_write_concern_get_w (new_wc ) > 1 ||
1889- mongoc_write_concern_get_wmajority (new_wc ) ||
1890- mongoc_write_concern_get_wtag (new_wc )) {
1891- mongoc_write_concern_set_wtimeout (new_wc , wtimeoutms );
1892- } else {
1893- mongoc_write_concern_set_wtimeout (new_wc , 0 );
1894- }
1895-
18961896 if (mongoc_write_concern_get_journal (new_wc )) {
18971897 int32_t w = mongoc_write_concern_get_w (new_wc );
18981898
0 commit comments