Skip to content

Commit 04765b4

Browse files
committed
PHPC-741: Consistent exceptions for ObjectID init methods
1 parent 6825553 commit 04765b4

7 files changed

+130
-16
lines changed

src/BSON/ObjectID.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ PHONGO_API zend_class_entry *php_phongo_objectid_ce;
4646

4747
zend_object_handlers php_phongo_handler_objectid;
4848

49-
/* Initialize the object with a generated value and return whether it was successful. */
49+
/* Initialize the object with a generated value and return whether it was
50+
* successful. */
5051
static bool php_phongo_objectid_init(php_phongo_objectid_t *intern)
5152
{
5253
bson_oid_t oid;
@@ -59,8 +60,9 @@ static bool php_phongo_objectid_init(php_phongo_objectid_t *intern)
5960
return true;
6061
}
6162

62-
/* Initialize the object from a hex string and return whether it was successful. */
63-
static bool php_phongo_objectid_init_from_hex_string(php_phongo_objectid_t *intern, const char *oid, phongo_zpp_char_len oid_len)
63+
/* Initialize the object from a hex string and return whether it was successful.
64+
* An exception will be thrown on error. */
65+
static bool php_phongo_objectid_init_from_hex_string(php_phongo_objectid_t *intern, const char *oid, phongo_zpp_char_len oid_len TSRMLS_DC)
6466
{
6567
char *tid = zend_str_tolower_dup(oid, oid_len);
6668

@@ -75,28 +77,33 @@ static bool php_phongo_objectid_init_from_hex_string(php_phongo_objectid_t *inte
7577
return true;
7678
}
7779

80+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Invalid BSON ID provided");
81+
7882
efree(tid);
7983
return false;
8084
}
8185

82-
/* Initialize the object from a HashTable and return whether it was successful. */
83-
static bool php_phongo_objectid_init_from_hash(php_phongo_objectid_t *intern, HashTable *props)
86+
/* Initialize the object from a HashTable and return whether it was successful.
87+
* An exception will be thrown on error. */
88+
static bool php_phongo_objectid_init_from_hash(php_phongo_objectid_t *intern, HashTable *props TSRMLS_DC)
8489
{
8590
#if PHP_VERSION_ID >= 70000
8691
zval *z_oid;
8792

8893
z_oid = zend_hash_str_find(props, "oid", sizeof("oid")-1);
8994

9095
if (z_oid && Z_TYPE_P(z_oid) == IS_STRING) {
91-
return php_phongo_objectid_init_from_hex_string(intern, Z_STRVAL_P(z_oid), Z_STRLEN_P(z_oid));
96+
return php_phongo_objectid_init_from_hex_string(intern, Z_STRVAL_P(z_oid), Z_STRLEN_P(z_oid) TSRMLS_CC);
9297
}
9398
#else
9499
zval **z_oid;
95100

96101
if (zend_hash_find(props, "oid", sizeof("oid"), (void**) &z_oid) == SUCCESS && Z_TYPE_PP(z_oid) == IS_STRING) {
97-
return php_phongo_objectid_init_from_hex_string(intern, Z_STRVAL_PP(z_oid), Z_STRLEN_PP(z_oid));
102+
return php_phongo_objectid_init_from_hex_string(intern, Z_STRVAL_PP(z_oid), Z_STRLEN_PP(z_oid) TSRMLS_CC);
98103
}
99104
#endif
105+
106+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"oid\" string field", ZSTR_VAL(php_phongo_objectid_ce->name));
100107
return false;
101108
}
102109

@@ -120,9 +127,7 @@ PHP_METHOD(ObjectID, __construct)
120127
zend_restore_error_handling(&error_handling TSRMLS_CC);
121128

122129
if (id) {
123-
if (!php_phongo_objectid_init_from_hex_string(intern, id, id_len)) {
124-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s", "Invalid BSON ID provided");
125-
}
130+
php_phongo_objectid_init_from_hex_string(intern, id, id_len TSRMLS_CC);
126131
} else {
127132
php_phongo_objectid_init(intern);
128133
}
@@ -146,9 +151,7 @@ PHP_METHOD(ObjectID, __set_state)
146151
intern = Z_OBJECTID_OBJ_P(return_value);
147152
props = Z_ARRVAL_P(array);
148153

149-
if (!php_phongo_objectid_init_from_hash(intern, props)) {
150-
php_error(E_ERROR, "Invalid serialization data for ObjectID object");
151-
}
154+
php_phongo_objectid_init_from_hash(intern, props TSRMLS_CC);
152155
}
153156
/* }}} */
154157

@@ -184,9 +187,7 @@ PHP_METHOD(ObjectID, __wakeup)
184187
intern = Z_OBJECTID_OBJ_P(getThis());
185188
props = zend_std_get_properties(getThis() TSRMLS_CC);
186189

187-
if (!php_phongo_objectid_init_from_hash(intern, props)) {
188-
php_error(E_ERROR, "Invalid serialization data for ObjectID object");
189-
}
190+
php_phongo_objectid_init_from_hash(intern, props TSRMLS_CC);
190191
}
191192
/* }}} */
192193

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectID unserialization requires "oid" string field
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:21:"MongoDB\BSON\ObjectID":1:{s:3:"oid";i:0;}');
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
18+
MongoDB\BSON\ObjectID initialization requires "oid" string field
19+
===DONE===
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectID unserialization requires valid hex string
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:21:"MongoDB\BSON\ObjectID":1:{s:3:"oid";s:24:"0123456789abcdefghijklmn";}');
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
echo throws(function() {
14+
unserialize('O:21:"MongoDB\BSON\ObjectID":1:{s:3:"oid";s:7:"INVALID";}');
15+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
22+
Invalid BSON ID provided
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Invalid BSON ID provided
25+
===DONE===
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectID::__set_state() requires "oid" string field
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\ObjectID::__set_state(['oid' => 0]);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
18+
MongoDB\BSON\ObjectID initialization requires "oid" string field
19+
===DONE===
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectID::__set_state() requires valid hex string
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\ObjectID::__set_state(['oid' => '0123456789abcdefghijklmn']);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
echo throws(function() {
14+
MongoDB\BSON\ObjectID::__set_state(['oid' => 'INVALID']);
15+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
22+
Invalid BSON ID provided
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Invalid BSON ID provided
25+
===DONE===
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectID::__construct() requires valid hex string
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+
new MongoDB\BSON\ObjectID('0123456789abcdefghijklmn');
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
echo throws(function() {
14+
new MongoDB\BSON\ObjectID('INVALID');
15+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
22+
Invalid BSON ID provided
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Invalid BSON ID provided
25+
===DONE===

0 commit comments

Comments
 (0)