Skip to content

Commit 0aa9add

Browse files
committed
CDRIVER-366: Fix enum related casting
1 parent 666f396 commit 0aa9add

File tree

5 files changed

+33
-40
lines changed

5 files changed

+33
-40
lines changed

src/mongoc/mongoc-bulk-operation.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,15 @@ mongoc_bulk_operation_replace_one (mongoc_bulk_operation_t *bulk,
237237
mongoc_write_command_t command = { 0 };
238238
size_t err_off;
239239
mongoc_write_command_t *last;
240+
int flags = BSON_VALIDATE_DOT_KEYS|BSON_VALIDATE_DOLLAR_KEYS;
240241

241242
bson_return_if_fail (bulk);
242243
bson_return_if_fail (selector);
243244
bson_return_if_fail (document);
244245

245246
ENTRY;
246247

247-
if (!bson_validate (document,
248-
(BSON_VALIDATE_DOT_KEYS | BSON_VALIDATE_DOLLAR_KEYS),
249-
&err_off)) {
248+
if (!bson_validate (document, (bson_validate_flags_t)flags, &err_off)) {
250249
MONGOC_WARNING ("%s(): replacement document may not contain "
251250
"$ or . in keys. Ignoring document.",
252251
__FUNCTION__);

src/mongoc/mongoc-collection.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,9 @@ _mongoc_collection_create_index_legacy (mongoc_collection_t *collection,
763763
col = mongoc_client_get_collection (collection->client, collection->db,
764764
"system.indexes");
765765

766-
ret = mongoc_collection_insert (col, MONGOC_INSERT_NO_VALIDATE, &insert, NULL,
767-
error);
766+
ret = mongoc_collection_insert (col,
767+
(mongoc_insert_flags_t)MONGOC_INSERT_NO_VALIDATE,
768+
&insert, NULL, error);
768769

769770
mongoc_collection_destroy(col);
770771

@@ -1062,14 +1063,11 @@ mongoc_collection_insert_bulk (mongoc_collection_t *collection,
10621063

10631064
if (!(flags & MONGOC_INSERT_NO_VALIDATE)) {
10641065
int i;
1066+
int vflags = (BSON_VALIDATE_UTF8 | BSON_VALIDATE_UTF8_ALLOW_NULL
1067+
| BSON_VALIDATE_DOLLAR_KEYS | BSON_VALIDATE_DOT_KEYS);
10651068

10661069
for (i = 0; i < n_documents; i++) {
1067-
if (!bson_validate (documents[i],
1068-
(BSON_VALIDATE_UTF8 |
1069-
BSON_VALIDATE_UTF8_ALLOW_NULL |
1070-
BSON_VALIDATE_DOLLAR_KEYS |
1071-
BSON_VALIDATE_DOT_KEYS),
1072-
NULL)) {
1070+
if (!bson_validate (documents[i], (bson_validate_flags_t)vflags, NULL)) {
10731071
bson_set_error (error,
10741072
MONGOC_ERROR_BSON,
10751073
MONGOC_ERROR_BSON_INVALID,
@@ -1153,21 +1151,17 @@ mongoc_collection_insert (mongoc_collection_t *collection,
11531151
}
11541152

11551153
if (!(flags & MONGOC_INSERT_NO_VALIDATE)) {
1156-
if (!bson_validate (document,
1157-
(BSON_VALIDATE_UTF8 |
1158-
BSON_VALIDATE_UTF8_ALLOW_NULL |
1159-
BSON_VALIDATE_DOLLAR_KEYS |
1160-
BSON_VALIDATE_DOT_KEYS),
1161-
NULL)) {
1154+
int vflags = (BSON_VALIDATE_UTF8 | BSON_VALIDATE_UTF8_ALLOW_NULL
1155+
| BSON_VALIDATE_DOLLAR_KEYS | BSON_VALIDATE_DOT_KEYS);
1156+
1157+
if (!bson_validate (document, (bson_validate_flags_t)vflags, NULL)) {
11621158
bson_set_error (error,
11631159
MONGOC_ERROR_BSON,
11641160
MONGOC_ERROR_BSON_INVALID,
11651161
"A document was corrupt or contained "
11661162
"invalid characters . or $");
11671163
RETURN (false);
11681164
}
1169-
} else {
1170-
flags &= ~MONGOC_INSERT_NO_VALIDATE;
11711165
}
11721166

11731167
_mongoc_write_result_init (&result);
@@ -1214,7 +1208,7 @@ mongoc_collection_insert (mongoc_collection_t *collection,
12141208

12151209
bool
12161210
mongoc_collection_update (mongoc_collection_t *collection,
1217-
mongoc_update_flags_t flags,
1211+
mongoc_update_flags_t uflags,
12181212
const bson_t *selector,
12191213
const bson_t *update,
12201214
const mongoc_write_concern_t *write_concern,
@@ -1225,6 +1219,9 @@ mongoc_collection_update (mongoc_collection_t *collection,
12251219
bson_iter_t iter;
12261220
size_t err_offset;
12271221
bool ret;
1222+
int vflags = (BSON_VALIDATE_UTF8 | BSON_VALIDATE_UTF8_ALLOW_NULL
1223+
| BSON_VALIDATE_DOLLAR_KEYS | BSON_VALIDATE_DOT_KEYS);
1224+
int flags = uflags;
12281225

12291226
ENTRY;
12301227

@@ -1242,12 +1239,7 @@ mongoc_collection_update (mongoc_collection_t *collection,
12421239
bson_iter_init (&iter, update) &&
12431240
bson_iter_next (&iter) &&
12441241
(bson_iter_key (&iter) [0] != '$') &&
1245-
!bson_validate (update,
1246-
(BSON_VALIDATE_UTF8 |
1247-
BSON_VALIDATE_UTF8_ALLOW_NULL |
1248-
BSON_VALIDATE_DOLLAR_KEYS |
1249-
BSON_VALIDATE_DOT_KEYS),
1250-
&err_offset)) {
1242+
!bson_validate (update, (bson_validate_flags_t)vflags, &err_offset)) {
12511243
bson_set_error (error,
12521244
MONGOC_ERROR_BSON,
12531245
MONGOC_ERROR_BSON_INVALID,

src/mongoc/mongoc-cursor.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ _mongoc_n_return (mongoc_cursor_t * cursor)
9898
mongoc_cursor_t *
9999
_mongoc_cursor_new (mongoc_client_t *client,
100100
const char *db_and_collection,
101-
mongoc_query_flags_t flags,
101+
mongoc_query_flags_t qflags,
102102
uint32_t skip,
103103
uint32_t limit,
104104
uint32_t batch_size,
@@ -117,6 +117,7 @@ _mongoc_cursor_new (mongoc_client_t *client,
117117
bson_t child;
118118
bool found = false;
119119
int i;
120+
int flags = qflags;
120121

121122
ENTRY;
122123

@@ -179,7 +180,7 @@ _mongoc_cursor_new (mongoc_client_t *client,
179180
cursor->client = client;
180181
bson_strncpy (cursor->ns, db_and_collection, sizeof cursor->ns);
181182
cursor->nslen = (uint32_t)strlen(cursor->ns);
182-
cursor->flags = flags;
183+
cursor->flags = (mongoc_query_flags_t)flags;
183184
cursor->skip = skip;
184185
cursor->limit = limit;
185186
cursor->batch_size = batch_size;

src/mongoc/mongoc-read-prefs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mongoc_read_prefs_new (mongoc_read_mode_t mode)
3737
mongoc_read_mode_t
3838
mongoc_read_prefs_get_mode (const mongoc_read_prefs_t *read_prefs)
3939
{
40-
bson_return_val_if_fail(read_prefs, 0);
40+
bson_return_val_if_fail(read_prefs, MONGOC_READ_PRIMARY);
4141
return read_prefs->mode;
4242
}
4343

src/mongoc/mongoc-write-command.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,8 @@ _mongoc_write_command_update_legacy (mongoc_write_command_t *command,
632632
bool val = false;
633633
char ns [MONGOC_NAMESPACE_MAX + 1];
634634
int32_t affected = 0;
635+
int vflags = (BSON_VALIDATE_UTF8 | BSON_VALIDATE_UTF8_ALLOW_NULL
636+
| BSON_VALIDATE_DOLLAR_KEYS | BSON_VALIDATE_DOT_KEYS);
635637

636638
ENTRY;
637639

@@ -652,12 +654,7 @@ _mongoc_write_command_update_legacy (mongoc_write_command_t *command,
652654
if (bson_iter_init (&subsubiter, &doc) &&
653655
bson_iter_next (&subsubiter) &&
654656
(bson_iter_key (&subsubiter) [0] != '$') &&
655-
!bson_validate (&doc,
656-
(BSON_VALIDATE_UTF8 |
657-
BSON_VALIDATE_UTF8_ALLOW_NULL |
658-
BSON_VALIDATE_DOLLAR_KEYS |
659-
BSON_VALIDATE_DOT_KEYS),
660-
&err_offset)) {
657+
!bson_validate (&doc, (bson_validate_flags_t)vflags, &err_offset)) {
661658
result->failed = true;
662659
bson_set_error (error,
663660
MONGOC_ERROR_BSON,
@@ -686,7 +683,7 @@ _mongoc_write_command_update_legacy (mongoc_write_command_t *command,
686683
rpc.update.opcode = MONGOC_OPCODE_UPDATE;
687684
rpc.update.zero = 0;
688685
rpc.update.collection = ns;
689-
rpc.update.flags = 0;
686+
rpc.update.flags = MONGOC_UPDATE_NONE;
690687

691688
has_update = false;
692689
has_selector = false;
@@ -706,12 +703,16 @@ _mongoc_write_command_update_legacy (mongoc_write_command_t *command,
706703
has_selector = true;
707704
} else if (strcmp (bson_iter_key (&subiter), "multi") == 0) {
708705
val = bson_iter_bool (&subiter);
709-
rpc.update.flags = rpc.update.flags |
710-
(val ? MONGOC_UPDATE_MULTI_UPDATE : 0);
706+
if (val) {
707+
rpc.update.flags = (mongoc_update_flags_t)(
708+
rpc.update.flags | MONGOC_UPDATE_MULTI_UPDATE);
709+
}
711710
} else if (strcmp (bson_iter_key (&subiter), "upsert") == 0) {
712711
val = bson_iter_bool (&subiter);
713-
rpc.update.flags = rpc.update.flags |
714-
(val ? MONGOC_UPDATE_UPSERT : 0);
712+
if (val) {
713+
rpc.update.flags = (mongoc_update_flags_t)(
714+
rpc.update.flags | MONGOC_UPDATE_UPSERT);
715+
}
715716
is_upsert = true;
716717
}
717718
}

0 commit comments

Comments
 (0)