Skip to content

Commit 7d3dadb

Browse files
committed
PHPC-741: Consistent exceptions for Binary init methods
1 parent 5c65399 commit 7d3dadb

7 files changed

+126
-15
lines changed

src/BSON/Binary.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ PHONGO_API zend_class_entry *php_phongo_binary_ce;
4646

4747
zend_object_handlers php_phongo_handler_binary;
4848

49-
/* Initialize the object from a string and return whether it was successful. */
50-
static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data, phongo_zpp_char_len data_len, phongo_long type)
49+
/* Initialize the object and return whether it was successful. An exception will
50+
* be thrown on error. */
51+
static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data, phongo_zpp_char_len data_len, phongo_long type TSRMLS_DC)
5152
{
5253
if (type < 0 || type > UINT8_MAX) {
54+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected type to be an unsigned 8-bit integer, %" PHONGO_LONG_FORMAT " given", type);
5355
return false;
5456
}
5557

@@ -60,24 +62,27 @@ static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data
6062
return true;
6163
}
6264

63-
/* Initialize the object from a HashTable and return whether it was successful. */
64-
static bool php_phongo_binary_init_from_hash(php_phongo_binary_t *intern, HashTable *props)
65+
/* Initialize the object from a HashTable and return whether it was successful.
66+
* An exception will be thrown on error. */
67+
static bool php_phongo_binary_init_from_hash(php_phongo_binary_t *intern, HashTable *props TSRMLS_DC)
6568
{
6669
#if PHP_VERSION_ID >= 70000
6770
zval *data, *type;
6871

6972
if ((data = zend_hash_str_find(props, "data", sizeof("data")-1)) && Z_TYPE_P(data) == IS_STRING &&
7073
(type = zend_hash_str_find(props, "type", sizeof("type")-1)) && Z_TYPE_P(type) == IS_LONG) {
71-
return php_phongo_binary_init(intern, Z_STRVAL_P(data), Z_STRLEN_P(data), Z_LVAL_P(type));
74+
return php_phongo_binary_init(intern, Z_STRVAL_P(data), Z_STRLEN_P(data), Z_LVAL_P(type) TSRMLS_CC);
7275
}
7376
#else
7477
zval **data, **type;
7578

7679
if (zend_hash_find(props, "data", sizeof("data"), (void**) &data) == SUCCESS && Z_TYPE_PP(data) == IS_STRING &&
7780
zend_hash_find(props, "type", sizeof("type"), (void**) &type) == SUCCESS && Z_TYPE_PP(type) == IS_LONG) {
78-
return php_phongo_binary_init(intern, Z_STRVAL_PP(data), Z_STRLEN_PP(data), Z_LVAL_PP(type));
81+
return php_phongo_binary_init(intern, Z_STRVAL_PP(data), Z_STRLEN_PP(data), Z_LVAL_PP(type) TSRMLS_CC);
7982
}
8083
#endif
84+
85+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"data\" string and \"type\" integer fields", ZSTR_VAL(php_phongo_binary_ce->name));
8186
return false;
8287
}
8388

@@ -101,9 +106,7 @@ PHP_METHOD(Binary, __construct)
101106
}
102107
zend_restore_error_handling(&error_handling TSRMLS_CC);
103108

104-
if (!php_phongo_binary_init(intern, data, data_len, type)) {
105-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected type to be an unsigned 8-bit integer, %" PHONGO_LONG_FORMAT " given", type);
106-
}
109+
php_phongo_binary_init(intern, data, data_len, type TSRMLS_CC);
107110
}
108111
/* }}} */
109112

@@ -124,9 +127,7 @@ PHP_METHOD(Binary, __set_state)
124127
intern = Z_BINARY_OBJ_P(return_value);
125128
props = Z_ARRVAL_P(array);
126129

127-
if (!php_phongo_binary_init_from_hash(intern, props)) {
128-
php_error(E_ERROR, "Invalid serialization data for Binary object");
129-
}
130+
php_phongo_binary_init_from_hash(intern, props TSRMLS_CC);
130131
}
131132
/* }}} */
132133

@@ -144,9 +145,7 @@ PHP_METHOD(Binary, __wakeup)
144145
intern = Z_BINARY_OBJ_P(getThis());
145146
props = zend_std_get_properties(getThis() TSRMLS_CC);
146147

147-
if (!php_phongo_binary_init_from_hash(intern, props)) {
148-
php_error(E_ERROR, "Invalid serialization data for Binary object");
149-
}
148+
php_phongo_binary_init_from_hash(intern, props TSRMLS_CC);
150149
}
151150
/* }}} */
152151

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
MongoDB\BSON\Binary unserialization requires "data" string and "type" integer fields
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
echo throws(function() {
10+
unserialize('O:19:"MongoDB\BSON\Binary":1:{s:4:"data";s:6:"foobar";}');
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
echo throws(function() {
14+
unserialize('O:19:"MongoDB\BSON\Binary":1:{s:4:"type";i:0;}');
15+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
17+
echo throws(function() {
18+
unserialize('O:19:"MongoDB\BSON\Binary":1:{s:4:"data";i:0;s:4:"type";s:6:"foobar";}');
19+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
20+
21+
?>
22+
===DONE===
23+
<?php exit(0); ?>
24+
--EXPECT--
25+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
26+
MongoDB\BSON\Binary initialization requires "data" string and "type" integer fields
27+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
28+
MongoDB\BSON\Binary initialization requires "data" string and "type" integer fields
29+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
30+
MongoDB\BSON\Binary initialization requires "data" string and "type" integer fields
31+
===DONE===
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
MongoDB\BSON\Binary unserialization requires unsigned 8-bit integer for type
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
echo throws(function() {
10+
unserialize('O:19:"MongoDB\BSON\Binary":2:{s:4:"data";s:6:"foobar";s:4:"type";i:-1;}');
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
echo throws(function() {
14+
unserialize('O:19:"MongoDB\BSON\Binary":2:{s:4:"data";s:6:"foobar";s:4:"type";i:256;}');
15+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
22+
Expected type to be an unsigned 8-bit integer, -1 given
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Expected type to be an unsigned 8-bit integer, 256 given
25+
===DONE===
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
MongoDB\BSON\Binary::__set_state() requires "data" string and "type" integer fields
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
echo throws(function() {
10+
MongoDB\BSON\Binary::__set_state(['data' => 'foobar']);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
echo throws(function() {
14+
MongoDB\BSON\Binary::__set_state(['type' => 0]);
15+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
17+
echo throws(function() {
18+
MongoDB\BSON\Binary::__set_state(['data' => 0, 'type' => 'foobar']);
19+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
20+
21+
?>
22+
===DONE===
23+
<?php exit(0); ?>
24+
--EXPECT--
25+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
26+
MongoDB\BSON\Binary initialization requires "data" string and "type" integer fields
27+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
28+
MongoDB\BSON\Binary initialization requires "data" string and "type" integer fields
29+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
30+
MongoDB\BSON\Binary initialization requires "data" string and "type" integer fields
31+
===DONE===
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
MongoDB\BSON\Binary::__set_state() requires unsigned 8-bit integer for type
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
echo throws(function() {
10+
MongoDB\BSON\Binary::__set_state(['data' => 'foobar', 'type' => -1]);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
echo throws(function() {
14+
MongoDB\BSON\Binary::__set_state(['data' => 'foobar', 'type' => 256]);
15+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
22+
Expected type to be an unsigned 8-bit integer, -1 given
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Expected type to be an unsigned 8-bit integer, 256 given
25+
===DONE===

0 commit comments

Comments
 (0)