Skip to content

PHPC-613, PHPC-615: Improve toJSON() error handling #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1583,10 +1583,19 @@ PHP_FUNCTION(toJSON)
char *str;
size_t str_len;
str = bson_as_json(b, &str_len);

if (!str) {
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not convert BSON document to a JSON string");
bson_reader_destroy(reader);
return;
}

PHONGO_RETVAL_STRINGL(str, str_len);
bson_free(str);
} else {
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Could not read document from BSON reader");
bson_reader_destroy(reader);
return;
}

if (bson_reader_read(reader, &eof) || !eof) {
Expand Down
4 changes: 2 additions & 2 deletions tests/bson/bson-toJSON_error-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ BSON\toJSON(): BSON decoding exceptions
<?php
require_once __DIR__ . "/../utils/basic.inc";

/* We can't really test for bson_iter_init() failure, since bson_reader_read()
* already checks that the buffer is at least 5 bytes.
/* We can't really test for bson_iter_init() failure within bson_as_json(),
* since bson_reader_read() already checks that the buffer is at least 5 bytes.
*/
$invalidBson = array(
'',
Expand Down
28 changes: 28 additions & 0 deletions tests/bson/bson-toJSON_error-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
BSON\toJSON(): BSON decoding exceptions for malformed documents
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$tests = array(
pack('Vx', 4), // Empty document with invalid length (too small)
pack('Vx', 6), // Empty document with invalid length (too large)
);

foreach ($tests as $bson) {
echo throws(function() use ($bson) {
toJSON($bson);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Could not read document from BSON reader
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Could not read document from BSON reader
===DONE===
39 changes: 39 additions & 0 deletions tests/bson/bson-toJSON_error-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
BSON\toJSON(): BSON decoding exceptions for bson_as_json() failure
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$tests = array(
// Invalid UTF-8 characters (i.e. 0xFE, 0xFF) in field name
pack('VCCCxVa*xx', 17, 2, 254, 255, 3, 'bar'),
/* Note: we don't use a three-character string in the underflow case, as
* the 4-byte string length and payload (i.e. three characters + null byte)
* coincidentally satisfy the expected size for an 8-byte double. We also
* don't use a four-character string, since its null byte would be
* interpreted as the document terminator. The actual document terminator
* would then remain in the buffer and trigger a "did not exhaust" error.
*/
pack('VCa*xVa*xx', 17, 1, 'foo', 3, 'ab'), // Invalid field type (underflow)
pack('VCa*xVa*xx', 20, 1, 'foo', 6, 'abcde'), // Invalid field type (overflow)
);

foreach ($tests as $bson) {
echo throws(function() use ($bson) {
toJSON($bson);
}, 'MongoDB\Driver\Exception\UnexpectedValueException'), "\n";
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Could not convert BSON document to a JSON string
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Could not convert BSON document to a JSON string
OK: Got MongoDB\Driver\Exception\UnexpectedValueException
Could not convert BSON document to a JSON string
===DONE===