From dd1a758f00f22f8d5ba269650b4c25a177510b84 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Mon, 21 Nov 2016 14:06:35 -0500 Subject: [PATCH] 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. --- src/BSON/Regex.c | 11 +++++++++++ tests/bson/bson-regex-005.phpt | 20 ++++++++++++++++++++ tests/bson/bson-regex-serialization-003.phpt | 18 ++++++++++++++++++ tests/bson/bson-regex-set_state-002.phpt | 20 ++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 tests/bson/bson-regex-005.phpt create mode 100644 tests/bson/bson-regex-serialization-003.phpt create mode 100644 tests/bson/bson-regex-set_state-002.phpt diff --git a/src/BSON/Regex.c b/src/BSON/Regex.c index 343bf14ba..b5e508b9b 100644 --- a/src/BSON/Regex.c +++ b/src/BSON/Regex.c @@ -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) @@ -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; diff --git a/tests/bson/bson-regex-005.phpt b/tests/bson/bson-regex-005.phpt new file mode 100644 index 000000000..864b526f4 --- /dev/null +++ b/tests/bson/bson-regex-005.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\BSON\Regex initialization will alphabetize flags +--FILE-- + +===DONE=== + +--EXPECTF-- +object(MongoDB\BSON\Regex)#%d (%d) { + ["pattern"]=> + string(6) "regexp" + ["flags"]=> + string(6) "ilmsux" +} +===DONE=== diff --git a/tests/bson/bson-regex-serialization-003.phpt b/tests/bson/bson-regex-serialization-003.phpt new file mode 100644 index 000000000..b7fa6c67e --- /dev/null +++ b/tests/bson/bson-regex-serialization-003.phpt @@ -0,0 +1,18 @@ +--TEST-- +MongoDB\BSON\Regex unserialization will alphabetize flags +--FILE-- + +===DONE=== + +--EXPECTF-- +object(MongoDB\BSON\Regex)#%d (%d) { + ["pattern"]=> + string(6) "regexp" + ["flags"]=> + string(6) "ilmsux" +} +===DONE=== diff --git a/tests/bson/bson-regex-set_state-002.phpt b/tests/bson/bson-regex-set_state-002.phpt new file mode 100644 index 000000000..a99a28ea2 --- /dev/null +++ b/tests/bson/bson-regex-set_state-002.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\BSON\Regex::__set_state() will alphabetize flags +--FILE-- + 'regexp', + 'flags' => 'xusmli', +])); +echo "\n"; + +?> +===DONE=== + +--EXPECTF-- +MongoDB\BSON\Regex::__set_state(array( +%w'pattern' => 'regexp', +%w'flags' => 'ilmsux', +)) +===DONE===