@@ -183,7 +183,13 @@ void php_phongo_bson_visit_corrupt(const bson_iter_t *iter ARG_UNUSED, void *dat
183
183
{
184
184
mongoc_log (MONGOC_LOG_LEVEL_TRACE , MONGOC_LOG_DOMAIN , "Corrupt BSON data detected!" );
185
185
}
186
- /* }}} */
186
+
187
+ void php_phongo_bson_visit_unsupported_type (const bson_iter_t * iter ARG_UNUSED , const char * key , uint32_t v_type_code , void * data ARG_UNUSED ) /* {{{ */
188
+ {
189
+ TSRMLS_FETCH ();
190
+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC , "Detected unknown BSON type 0x%02hhx for fieldname \"%s\". Are you using the latest driver?" , v_type_code , key );
191
+ }
192
+
187
193
bool php_phongo_bson_visit_double (const bson_iter_t * iter ARG_UNUSED , const char * key , double v_double , void * data ) /* {{{ */
188
194
{
189
195
#if PHP_VERSION_ID >= 70000
@@ -574,7 +580,7 @@ static const bson_visitor_t php_bson_visitors = {
574
580
php_phongo_bson_visit_int64 ,
575
581
php_phongo_bson_visit_maxkey ,
576
582
php_phongo_bson_visit_minkey ,
577
- NULL /* php_phongo_bson_visit_unsupported_type*/ ,
583
+ php_phongo_bson_visit_unsupported_type ,
578
584
NULL /*php_phongo_bson_visit_decimal128*/ ,
579
585
{ NULL }
580
586
};
@@ -1401,8 +1407,12 @@ PHONGO_API int phongo_bson_to_zval_ex(const unsigned char *data, int data_len, p
1401
1407
if (bson_iter_visit_all (& iter , & php_bson_visitors , state ) || iter .err_off ) {
1402
1408
/* Iteration stopped prematurely due to corruption or a failed visitor.
1403
1409
* While we free the reader, state->zchild should be left as-is, since
1404
- * the calling code may want to zval_ptr_dtor() it. */
1405
- phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC , "Could not convert BSON document to a PHP variable" );
1410
+ * the calling code may want to zval_ptr_dtor() it. If an exception has
1411
+ * been thrown already (due to an unsupported BSON type for example,
1412
+ * don't overwrite with a generic exception message. */
1413
+ if (!EG (exception )) {
1414
+ phongo_throw_exception (PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC , "Detected corrupt BSON data" );
1415
+ }
1406
1416
bson_reader_destroy (reader );
1407
1417
return 0 ;
1408
1418
}
0 commit comments