Permalink
Browse files

CDRIVER-2128 ensure regex options are sorted

Ensure that the regex option flags "ilmsux" are stored in sorted order,
even when passed to bson_append_regex.
  • Loading branch information...
ajdavis committed Jul 11, 2017
1 parent fcafbfb commit f9c179bbc0fe6049a73a46abac2fc3e27c832393
Showing with 72 additions and 21 deletions.
  1. +2 −0 src/bson/bson-private.h
  2. +28 −16 src/bson/bson.c
  3. +3 −4 tests/test-bcon-basic.c
  4. +9 −1 tests/test-bson-corpus.c
  5. +30 −0 tests/test-json.c
View
@@ -86,6 +86,8 @@ typedef struct {
BSON_STATIC_ASSERT (sizeof (bson_impl_alloc_t) <= 128);
#define BSON_REGEX_OPTIONS_SORTED "ilmsux"
BSON_END_DECLS
View
@@ -1512,7 +1512,9 @@ bson_append_regex (bson_t *bson,
{
static const uint8_t type = BSON_TYPE_REGEX;
uint32_t regex_len;
uint32_t options_len;
bson_string_t *options_sorted;
const char *c;
bool r;
BSON_ASSERT (bson);
BSON_ASSERT (key);
@@ -1530,21 +1532,31 @@ bson_append_regex (bson_t *bson,
}
regex_len = (int) strlen (regex) + 1;
options_len = (int) strlen (options) + 1;
options_sorted = bson_string_new (NULL);
return _bson_append (bson,
5,
(1 + key_length + 1 + regex_len + options_len),
1,
&type,
key_length,
key,
1,
&gZero,
regex_len,
regex,
options_len,
options);
for (c = BSON_REGEX_OPTIONS_SORTED; *c; c++) {
if (strchr (options, *c)) {
bson_string_append_c (options_sorted, *c);
}
}
r = _bson_append (bson,
5,
(1 + key_length + 1 + regex_len + options_sorted->len),
1,
&type,
key_length,
key,
1,
&gZero,
regex_len,
regex,
options_sorted->len + 1,
options_sorted->str);
bson_string_free (options_sorted, true);
return r;
}
@@ -2744,7 +2756,7 @@ _bson_as_json_visit_regex (const bson_iter_t *iter,
bson_string_append (state->str, "\", \"options\" : \"");
/* sort the options */
for (c = "ilmsux"; *c; c++) {
for (c = BSON_REGEX_OPTIONS_SORTED; *c; c++) {
if (strchr (v_options, *c)) {
bson_string_append_c (state->str, *c);
}
View
@@ -193,10 +193,9 @@ test_regex (void)
bson_init (&bcon);
bson_init (&expected);
bson_append_regex (&expected, "foo", -1, "^foo|bar$", "i");
BCON_APPEND (&bcon, "foo", BCON_REGEX ("^foo|bar$", "i"));
/* option flags are sorted */
bson_append_regex (&expected, "foo", -1, "^foo|bar$", "mis");
BCON_APPEND (&bcon, "foo", BCON_REGEX ("^foo|bar$", "msi"));
bson_eq_bson (&bcon, &expected);
bson_destroy (&bcon);
View
@@ -70,8 +70,11 @@ github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.rst
* bson_to_canonical_extended_json(dB) = cE
* bson_to_relaxed_extended_json(dB) = rE (if rE exists)
* for dE input (if it exists):
* json_to_bson(dE) = cB (unless lossy)
* for rE input (if it exists):
bson_to_relaxed_extended_json( json_to_bson(rE) ) = rE
bson_to_relaxed_extended_json(json_to_bson(rE)) = rE
*/
static void
@@ -133,6 +136,11 @@ test_bson_corpus (test_bson_type_t *test)
ASSERT_OR_PRINT (decode_dE, error);
ASSERT_CMPJSON (bson_as_extended_json (decode_dE, NULL), test->cE);
if (!test->lossy) {
compare_data (
bson_get_data (decode_dE), decode_dE->len, test->cB, test->cB_len);
}
bson_destroy (decode_dE);
}
View
@@ -841,6 +841,33 @@ test_bson_json_read_legacy_regex (void)
}
static void
test_bson_json_read_regex_options_order (void)
{
bson_t b;
bson_error_t error;
bool r;
const char *pattern;
const char *flags;
r = bson_init_from_json (
&b, "{\"a\": {\"$regex\": \"\", \"$options\": \"ism\"}}", -1, &error);
ASSERT_OR_PRINT (r, error);
BCON_EXTRACT (&b, "a", BCONE_REGEX (pattern, flags));
ASSERT_CMPSTR (flags, "ims");
bson_destroy (&b);
r = bson_init_from_json (
&b, "{\"a\": {\"$regex\": \"\", \"$options\": \"misl\"}}", -1, &error);
ASSERT_OR_PRINT (r, error);
BCON_EXTRACT (&b, "a", BCONE_REGEX (pattern, flags));
ASSERT_CMPSTR (flags, "ilms");
bson_destroy (&b);
}
static void
test_bson_json_read_binary (void)
{
@@ -2396,6 +2423,9 @@ test_json_install (TestSuite *suite)
suite, "/bson/json/read/dbpointer", test_bson_json_read_dbpointer);
TestSuite_Add (
suite, "/bson/json/read/legacy_regex", test_bson_json_read_legacy_regex);
TestSuite_Add (suite,
"/bson/json/read/regex_options_order",
test_bson_json_read_regex_options_order);
TestSuite_Add (suite, "/bson/json/read/binary", test_bson_json_read_binary);
TestSuite_Add (suite,
"/bson/json/read/legacy_binary",

0 comments on commit f9c179b

Please sign in to comment.