Skip to content

Commit 8d6c17d

Browse files
committed
PHPC-345: bson_to_zval() should throw exceptions instead of warnings
1 parent fa19ed8 commit 8d6c17d

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/bson.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,12 @@ int bson_to_zval(const unsigned char *data, int data_len, php_phongo_bson_state
897897
reader = bson_reader_new_from_data(data, data_len);
898898

899899
if (!(b = bson_reader_read(reader, NULL))) {
900-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not read document from reader");
900+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader");
901901
return 0;
902902
}
903903

904904
if (!bson_iter_init(&iter, b)) {
905-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize BSON iterator");
905+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not initialize BSON iterator");
906906
bson_reader_destroy(reader);
907907
return 0;
908908
}
@@ -941,7 +941,7 @@ int bson_to_zval(const unsigned char *data, int data_len, php_phongo_bson_state
941941
}
942942

943943
if (bson_reader_read(reader, &eof) || !eof) {
944-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Reading document did not exhaust input buffer");
944+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Reading document did not exhaust input buffer");
945945
bson_reader_destroy(reader);
946946
return 0;
947947
}

tests/bson/bson-toPHP_error-002.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
BSON\toPHP(): BSON decoding exceptions
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
/* We can't really test for bson_iter_init() failure, since bson_reader_read()
10+
* already checks that the buffer is at least 5 bytes.
11+
*/
12+
$invalidBson = array(
13+
'',
14+
str_repeat(fromJSON('{"x": "y"}'), 2),
15+
);
16+
17+
foreach ($invalidBson as $bson) {
18+
try {
19+
var_dump(toPHP($bson));
20+
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
21+
echo $e->getMessage(), "\n";
22+
}
23+
}
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECTF--
29+
Could not read document from BSON reader
30+
Reading document did not exhaust input buffer
31+
===DONE===

tests/bson/bug0325.phpt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ require_once __DIR__ . "/../utils/basic.inc";
88

99
$bson1 = fromJSON('{"x": "y"}');
1010
$bson2 = fromJSON('{"a": "b"}');
11-
$value = toPHP($bson1 . $bson2);
12-
13-
var_dump($value);
11+
try {
12+
var_dump(toPHP($bson1 . $bson2));
13+
} catch (MongoDB\Driver\Exception\UnexpectedValueException $e) {
14+
echo $e->getMessage(), "\n";
15+
}
1416

1517
?>
1618
===DONE===
1719
<?php exit(0); ?>
1820
--EXPECTF--
19-
Warning:%sReading document did not exhaust input buffer in %s on line %d%a
20-
NULL
21+
Reading document did not exhaust input buffer
2122
===DONE===

0 commit comments

Comments
 (0)