Skip to content

Commit dd1a758

Browse files
committed
PHPC-829: BSON Regex flags must be alphabetically ordered
We do not test that MongoDB\BSON\fromJSON() alphabetizes the flags, as that will be handled by CDRIVER-1883.
1 parent 74c3849 commit dd1a758

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

src/BSON/Regex.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ PHONGO_API zend_class_entry *php_phongo_regex_ce;
5353

5454
zend_object_handlers php_phongo_handler_regex;
5555

56+
/* qsort() compare callback for alphabetizing regex flags upon initialization */
57+
static int php_phongo_regex_compare_flags(const void *f1, const void *f2) {
58+
if (* (const char *) f1 == * (const char *) f2) {
59+
return 0;
60+
}
61+
62+
return (* (const char *) f1 > * (const char *) f2) ? 1 : -1;
63+
}
64+
5665
/* Initialize the object and return whether it was successful. An exception will
5766
* be thrown on error. */
5867
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 TSRMLS_DC)
@@ -71,6 +80,8 @@ static bool php_phongo_regex_init(php_phongo_regex_t *intern, const char *patter
7180
}
7281
intern->flags = estrndup(flags, flags_len);
7382
intern->flags_len = flags_len;
83+
/* Ensure flags are alphabetized upon initialization */
84+
qsort((void *) intern->flags, flags_len, 1, php_phongo_regex_compare_flags);
7485
} else {
7586
intern->flags = estrdup("");
7687
intern->flags_len = 0;

tests/bson/bson-regex-005.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
MongoDB\BSON\Regex initialization will alphabetize flags
3+
--FILE--
4+
<?php
5+
6+
$regex = new MongoDB\BSON\Regex('regexp', 'xusmli');
7+
8+
var_dump($regex);
9+
10+
?>
11+
===DONE===
12+
<?php exit(0); ?>
13+
--EXPECTF--
14+
object(MongoDB\BSON\Regex)#%d (%d) {
15+
["pattern"]=>
16+
string(6) "regexp"
17+
["flags"]=>
18+
string(6) "ilmsux"
19+
}
20+
===DONE===
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\BSON\Regex unserialization will alphabetize flags
3+
--FILE--
4+
<?php
5+
6+
var_dump(unserialize('C:18:"MongoDB\BSON\Regex":58:{a:2:{s:7:"pattern";s:6:"regexp";s:5:"flags";s:6:"xusmli";}}'));
7+
8+
?>
9+
===DONE===
10+
<?php exit(0); ?>
11+
--EXPECTF--
12+
object(MongoDB\BSON\Regex)#%d (%d) {
13+
["pattern"]=>
14+
string(6) "regexp"
15+
["flags"]=>
16+
string(6) "ilmsux"
17+
}
18+
===DONE===
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
MongoDB\BSON\Regex::__set_state() will alphabetize flags
3+
--FILE--
4+
<?php
5+
6+
var_export(MongoDB\BSON\Regex::__set_state([
7+
'pattern' => 'regexp',
8+
'flags' => 'xusmli',
9+
]));
10+
echo "\n";
11+
12+
?>
13+
===DONE===
14+
<?php exit(0); ?>
15+
--EXPECTF--
16+
MongoDB\BSON\Regex::__set_state(array(
17+
%w'pattern' => 'regexp',
18+
%w'flags' => 'ilmsux',
19+
))
20+
===DONE===

0 commit comments

Comments
 (0)