Skip to content

Commit 920e836

Browse files
committed
PHPC-475: toJSON and fromJSON should thrown on error
1 parent 9dbf277 commit 920e836

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/bson.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ PHP_FUNCTION(toJSON)
10961096
char *data;
10971097
int data_len;
10981098
const bson_t *b;
1099+
bool eof = false;
10991100
bson_reader_t *reader;
11001101

11011102
(void)return_value_ptr; (void)this_ptr; (void)return_value_used; /* We don't use these */
@@ -1114,8 +1115,13 @@ PHP_FUNCTION(toJSON)
11141115
RETVAL_STRINGL(str, str_len, 1);
11151116
bson_free(str);
11161117
} else {
1117-
RETURN_NULL();
1118+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader");
11181119
}
1120+
1121+
if (bson_reader_read(reader, &eof) || !eof) {
1122+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Reading document did not exhaust input buffer");
1123+
}
1124+
11191125
bson_reader_destroy(reader);
11201126
}
11211127
/* }}} */
@@ -1139,7 +1145,7 @@ PHP_FUNCTION(fromJSON)
11391145
RETVAL_STRINGL((const char *) bson_get_data(&b), b.len, 1);
11401146
bson_destroy(&b);
11411147
} else {
1142-
RETURN_NULL();
1148+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "%s", error.message ? error.message : "Error parsing JSON");
11431149
}
11441150
}
11451151
/* }}} */

tests/bson/bson-fromJSON_error-001.phpt

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ BSON\fromJSON(): invalid JSON
77

88
require_once __DIR__ . "/../utils/basic.inc";
99

10-
var_dump(fromJSON('foo'));
10+
echo throws(function() {
11+
fromJSON('foo');
12+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
1113

1214
?>
1315
===DONE===
1416
<?php exit(0); ?>
1517
--EXPECT--
16-
NULL
18+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
19+
lexical error: invalid string in json text.
20+
foo
21+
(right here) ------^
22+
1723
===DONE===

tests/bson/bson-toJSON_error-001.phpt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
BSON\toJSON(): 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+
echo throws(function() use ($bson) {
19+
toJSON($bson);
20+
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
21+
}
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECTF--
27+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
28+
Could not read document from BSON reader
29+
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
30+
Reading document did not exhaust input buffer
31+
===DONE===

0 commit comments

Comments
 (0)