Skip to content

Commit 888211c

Browse files
committed
Merged pull request #306
2 parents fc19a7d + a04008e commit 888211c

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

src/bson.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ void php_phongo_bson_visit_corrupt(const bson_iter_t *iter ARG_UNUSED, void *dat
183183
{
184184
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "Corrupt BSON data detected!");
185185
}
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+
187193
bool php_phongo_bson_visit_double(const bson_iter_t *iter ARG_UNUSED, const char *key, double v_double, void *data) /* {{{ */
188194
{
189195
#if PHP_VERSION_ID >= 70000
@@ -574,7 +580,7 @@ static const bson_visitor_t php_bson_visitors = {
574580
php_phongo_bson_visit_int64,
575581
php_phongo_bson_visit_maxkey,
576582
php_phongo_bson_visit_minkey,
577-
NULL /*php_phongo_bson_visit_unsupported_type*/,
583+
php_phongo_bson_visit_unsupported_type,
578584
NULL /*php_phongo_bson_visit_decimal128*/,
579585
{ NULL }
580586
};
@@ -1401,8 +1407,12 @@ PHONGO_API int phongo_bson_to_zval_ex(const unsigned char *data, int data_len, p
14011407
if (bson_iter_visit_all(&iter, &php_bson_visitors, state) || iter.err_off) {
14021408
/* Iteration stopped prematurely due to corruption or a failed visitor.
14031409
* 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+
}
14061416
bson_reader_destroy(reader);
14071417
return 0;
14081418
}

src/libbson

tests/bson/bson-toPHP_error-004.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ foreach ($tests as $bson) {
3535
<?php exit(0); ?>
3636
--EXPECTF--
3737
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
38-
Could not convert BSON document to a PHP variable
38+
Detected corrupt BSON data.
3939
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
40-
Could not convert BSON document to a PHP variable
40+
Detected corrupt BSON data.
4141
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
42-
Could not convert BSON document to a PHP variable
42+
Detected corrupt BSON data.
4343
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
44-
Could not convert BSON document to a PHP variable
44+
Detected corrupt BSON data.
4545
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
46-
Could not convert BSON document to a PHP variable
46+
Detected unknown BSON type 0x65 for fieldname "". Are you using the latest driver?
4747
===DONE===

tests/bson/bug0531.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ echo throws(function() use ($bson) {
1919
<?php exit(0); ?>
2020
--EXPECTF--
2121
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
22-
Could not convert BSON document to a PHP variable
22+
Detected unknown BSON type 0x31 for fieldname "hello". Are you using the latest driver?
2323
===DONE===

0 commit comments

Comments
 (0)