Skip to content

PHPC-829: BSON Regex flags must be alphabetically ordered #463

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 1 commit into from
Nov 22, 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
11 changes: 11 additions & 0 deletions src/BSON/Regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ PHONGO_API zend_class_entry *php_phongo_regex_ce;

zend_object_handlers php_phongo_handler_regex;

/* qsort() compare callback for alphabetizing regex flags upon initialization */
static int php_phongo_regex_compare_flags(const void *f1, const void *f2) {
if (* (const char *) f1 == * (const char *) f2) {
return 0;
}

return (* (const char *) f1 > * (const char *) f2) ? 1 : -1;
}

/* Initialize the object and return whether it was successful. An exception will
* be thrown on error. */
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)
Expand All @@ -71,6 +80,8 @@ static bool php_phongo_regex_init(php_phongo_regex_t *intern, const char *patter
}
intern->flags = estrndup(flags, flags_len);
intern->flags_len = flags_len;
/* Ensure flags are alphabetized upon initialization */
qsort((void *) intern->flags, flags_len, 1, php_phongo_regex_compare_flags);
} else {
intern->flags = estrdup("");
intern->flags_len = 0;
Expand Down
20 changes: 20 additions & 0 deletions tests/bson/bson-regex-005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
MongoDB\BSON\Regex initialization will alphabetize flags
--FILE--
<?php

$regex = new MongoDB\BSON\Regex('regexp', 'xusmli');

var_dump($regex);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
object(MongoDB\BSON\Regex)#%d (%d) {
["pattern"]=>
string(6) "regexp"
["flags"]=>
string(6) "ilmsux"
}
===DONE===
18 changes: 18 additions & 0 deletions tests/bson/bson-regex-serialization-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
MongoDB\BSON\Regex unserialization will alphabetize flags
--FILE--
<?php

var_dump(unserialize('C:18:"MongoDB\BSON\Regex":58:{a:2:{s:7:"pattern";s:6:"regexp";s:5:"flags";s:6:"xusmli";}}'));

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
object(MongoDB\BSON\Regex)#%d (%d) {
["pattern"]=>
string(6) "regexp"
["flags"]=>
string(6) "ilmsux"
}
===DONE===
20 changes: 20 additions & 0 deletions tests/bson/bson-regex-set_state-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
MongoDB\BSON\Regex::__set_state() will alphabetize flags
--FILE--
<?php

var_export(MongoDB\BSON\Regex::__set_state([
'pattern' => 'regexp',
'flags' => 'xusmli',
]));
echo "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
MongoDB\BSON\Regex::__set_state(array(
%w'pattern' => 'regexp',
%w'flags' => 'ilmsux',
))
===DONE===