Skip to content

Commit 160c65d

Browse files
committed
PHPC-741: Consistent exceptions for Regex init methods
1 parent 04765b4 commit 160c65d

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

src/BSON/Regex.c

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

4747
zend_object_handlers php_phongo_handler_regex;
4848

49-
/* Initialize the object from a string and return whether it was successful. */
49+
/* Initialize the object and return whether it was successful. */
5050
static bool php_phongo_regex_init(php_phongo_regex_t *intern, const char *pattern, phongo_zpp_char_len pattern_len, const char *flags, phongo_zpp_char_len flags_len)
5151
{
5252
intern->pattern = estrndup(pattern, pattern_len);
@@ -57,8 +57,9 @@ static bool php_phongo_regex_init(php_phongo_regex_t *intern, const char *patter
5757
return true;
5858
}
5959

60-
/* Initialize the object from a HashTable and return whether it was successful. */
61-
static bool php_phongo_regex_init_from_hash(php_phongo_regex_t *intern, HashTable *props)
60+
/* Initialize the object from a HashTable and return whether it was successful.
61+
* An exception will be thrown on error. */
62+
static bool php_phongo_regex_init_from_hash(php_phongo_regex_t *intern, HashTable *props TSRMLS_DC)
6263
{
6364
#if PHP_VERSION_ID >= 70000
6465
zval *pattern, *flags;
@@ -75,6 +76,8 @@ static bool php_phongo_regex_init_from_hash(php_phongo_regex_t *intern, HashTabl
7576
return php_phongo_regex_init(intern, Z_STRVAL_PP(pattern), Z_STRLEN_PP(pattern), Z_STRVAL_PP(flags), Z_STRLEN_PP(flags));
7677
}
7778
#endif
79+
80+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"pattern\" and \"flags\" string fields", ZSTR_VAL(php_phongo_regex_ce->name));
7881
return false;
7982
}
8083

@@ -154,9 +157,7 @@ PHP_METHOD(Regex, __set_state)
154157
intern = Z_REGEX_OBJ_P(return_value);
155158
props = Z_ARRVAL_P(array);
156159

157-
if (!php_phongo_regex_init_from_hash(intern, props)) {
158-
php_error(E_ERROR, "Invalid serialization data for Regex object");
159-
}
160+
php_phongo_regex_init_from_hash(intern, props TSRMLS_CC);
160161
}
161162
/* }}} */
162163

@@ -196,9 +197,7 @@ PHP_METHOD(Regex, __wakeup)
196197
intern = Z_REGEX_OBJ_P(getThis());
197198
props = zend_std_get_properties(getThis() TSRMLS_CC);
198199

199-
if (!php_phongo_regex_init_from_hash(intern, props)) {
200-
php_error(E_ERROR, "Invalid serialization data for Regex object");
201-
}
200+
php_phongo_regex_init_from_hash(intern, props TSRMLS_CC);
202201
}
203202
/* }}} */
204203

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
MongoDB\BSON\Regex unserialization requires "pattern" and "flags" string 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:18:"MongoDB\BSON\Regex":1:{s:7:"pattern";s:6:"regexp";}');
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
echo throws(function() {
14+
unserialize('O:18:"MongoDB\BSON\Regex":1:{s:5:"flags";s:1:"i";}');
15+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
17+
echo throws(function() {
18+
unserialize('O:18:"MongoDB\BSON\Regex":2:{s:7:"pattern";i:0;s:5:"flags";i:0;}');
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\Regex initialization requires "pattern" and "flags" string fields
27+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
28+
MongoDB\BSON\Regex initialization requires "pattern" and "flags" string fields
29+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
30+
MongoDB\BSON\Regex initialization requires "pattern" and "flags" string fields
31+
===DONE===
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
MongoDB\BSON\Regex::__set_state() requires "pattern" and "flags" string 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\Regex::__set_state(['pattern' => 'regexp']);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
echo throws(function() {
14+
MongoDB\BSON\Regex::__set_state(['flags' => 'i']);
15+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
16+
17+
echo throws(function() {
18+
MongoDB\BSON\Regex::__set_state(['pattern' => 0, 'flags' => 0]);
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\Regex initialization requires "pattern" and "flags" string fields
27+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
28+
MongoDB\BSON\Regex initialization requires "pattern" and "flags" string fields
29+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
30+
MongoDB\BSON\Regex initialization requires "pattern" and "flags" string fields
31+
===DONE===

0 commit comments

Comments
 (0)