Skip to content

Commit 3f8668d

Browse files
authored
MONGOCRYPT-783 address -Wpedantic warnings for anonymous structs and unions (#972)
1 parent 5aa9958 commit 3f8668d

17 files changed

+162
-163
lines changed

src/csfle-markup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ int do_main(int argc, const char *const *argv) {
102102

103103
mcr_dll csfle = mcr_dll_open(argv[1]);
104104
auto close_csfle = DEFER({ mcr_dll_close(csfle); });
105-
if (csfle.error_string.data) {
106-
std::cerr << "Failed to open [" << argv[1] << "] as a dynamic library: " << csfle.error_string.data << '\n';
105+
if (csfle.error_string.raw.data) {
106+
std::cerr << "Failed to open [" << argv[1] << "] as a dynamic library: " << csfle.error_string.raw.data << '\n';
107107
return 3;
108108
}
109109

src/mlib/path.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ static inline mstr mpath_current_path(void) {
7676
}
7777
#else
7878
mstr_mut mut = mstr_new(8096);
79-
char *p = getcwd(mut.data, mut.len);
79+
char *p = getcwd(mut.raw.data, mut.raw.len);
8080
if (p == NULL) {
8181
mstr_free(mut.mstr);
8282
return MSTR_NULL;
8383
}
84-
mstr ret = mstr_copy_cstr(mut.data);
84+
mstr ret = mstr_copy_cstr(mut.raw.data);
8585
mstr_free(mut.mstr);
8686
return ret;
8787
#endif
@@ -167,7 +167,7 @@ static inline mstr mpath_join(mstr_view base, mstr_view suffix, mpath_format f)
167167
// We must insert a path separator between the two strings
168168
assert(base.len <= SIZE_MAX - suffix.len - 1u);
169169
mstr_mut r = mstr_new(base.len + suffix.len + 1);
170-
char *p = r.data;
170+
char *p = r.raw.data;
171171
memcpy(p, base.data, base.len);
172172
p += base.len;
173173
*p++ = mpath_preferred_sep(f);
@@ -255,7 +255,7 @@ static inline mstr_view mpath_relative_path(mstr_view path, mpath_format f) {
255255
static inline mstr mpath_to_format(mpath_format from, mstr_view path, mpath_format to) {
256256
mstr_mut ret = mstr_new(path.len);
257257
const char *p = path.data;
258-
char *out = ret.data;
258+
char *out = ret.raw.data;
259259
const char *stop = path.data + path.len;
260260
for (; p != stop; ++p, ++out) {
261261
if (mpath_is_sep(*p, from)) {

src/mlib/str.h

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,26 @@ typedef struct mstr_view {
7676
* relinquish ownership of that `mstr` to the callee/caller, respectively.
7777
* Passing or returning an `mstr_view` is non-owning.
7878
*/
79-
typedef struct mstr {
80-
union {
81-
struct {
82-
/**
83-
* @brief Pointer to the beginning of the code unit array.
84-
*
85-
* @note DO NOT MODIFY
86-
*/
87-
const char *data;
88-
/**
89-
* @brief Length of the pointed-to code unit array
90-
*
91-
* @note DO NOT MODIFY
92-
*/
93-
size_t len;
94-
};
95-
79+
typedef union mstr {
80+
struct {
81+
/**
82+
* @brief Pointer to the beginning of the code unit array.
83+
*
84+
* @note DO NOT MODIFY
85+
*/
86+
const char *data;
9687
/**
97-
* @brief A non-owning `mstr_view` of the string
88+
* @brief Length of the pointed-to code unit array
89+
*
90+
* @note DO NOT MODIFY
9891
*/
99-
mstr_view view;
100-
};
92+
size_t len;
93+
} raw;
94+
95+
/**
96+
* @brief A non-owning `mstr_view` of the string
97+
*/
98+
mstr_view view;
10199
} mstr;
102100

103101
/**
@@ -106,34 +104,32 @@ typedef struct mstr {
106104
* Returned by @ref mstr_new(). Once initialization is complete, the result can
107105
* be used as an @ref mstr by accessing the @ref mstr_mut::mstr member.
108106
*/
109-
typedef struct mstr_mut {
110-
union {
111-
struct {
112-
/**
113-
* @brief Pointer to the beginning of the mutable code unit array.
114-
*
115-
* @note DO NOT MODIFY THE POINTER VALUE. Only modify the pointed-to
116-
* characters.
117-
*/
118-
char *data;
119-
/**
120-
* @brief Length of the pointed-to code unit array.
121-
*
122-
* @note DO NOT MODIFY
123-
*/
124-
size_t len;
125-
};
126-
/// Convert the mutable string to an immutable string
127-
struct mstr mstr;
128-
/// Convert the mutable string to an immutable string view
129-
mstr_view view;
130-
};
107+
typedef union mstr_mut {
108+
struct {
109+
/**
110+
* @brief Pointer to the beginning of the mutable code unit array.
111+
*
112+
* @note DO NOT MODIFY THE POINTER VALUE. Only modify the pointed-to
113+
* characters.
114+
*/
115+
char *data;
116+
/**
117+
* @brief Length of the pointed-to code unit array.
118+
*
119+
* @note DO NOT MODIFY
120+
*/
121+
size_t len;
122+
} raw;
123+
/// Convert the mutable string to an immutable string
124+
union mstr mstr;
125+
/// Convert the mutable string to an immutable string view
126+
mstr_view view;
131127
} mstr_mut;
132128

133129
/**
134130
* @brief A null @ref mstr
135131
*/
136-
#define MSTR_NULL (MLIB_INIT(mstr){{{NULL, 0}}})
132+
#define MSTR_NULL (MLIB_INIT(mstr){{NULL, 0}})
137133
/**
138134
* @brief A null @ref mstr_view
139135
*/
@@ -159,7 +155,7 @@ typedef struct mstr_mut {
159155
static inline mstr_mut mstr_new(size_t len) {
160156
#ifndef __clang_analyzer__
161157
assert(len < SIZE_MAX);
162-
return MLIB_INIT(mstr_mut){{{(char *)calloc(1, len + 1), len}}};
158+
return MLIB_INIT(mstr_mut){{(char *)calloc(1, len + 1), len}};
163159
#else
164160
// Clang-analyzer is smart enough to see the calloc(), but not smart enough
165161
// to link it to the free() in mstr_free()
@@ -201,7 +197,7 @@ static inline mstr_view mstrv_view_cstr(const char *s) {
201197
*/
202198
static inline mstr mstr_copy_data(const char *s, size_t len) {
203199
mstr_mut r = mstr_new(len);
204-
memcpy(r.data, s, len);
200+
memcpy(r.raw.data, s, len);
205201
return r.mstr;
206202
}
207203

@@ -231,7 +227,7 @@ static inline mstr mstr_copy(mstr_view s) {
231227
* @param s The string to free
232228
*/
233229
static inline void mstr_free(mstr s) {
234-
free((char *)s.data);
230+
free((char *)s.raw.data);
235231
}
236232

237233
/**
@@ -242,21 +238,21 @@ static inline void mstr_free(mstr s) {
242238
* @param new_len The new length of the string
243239
*/
244240
static inline void mstrm_resize(mstr_mut *s, size_t new_len) {
245-
if (new_len <= s->len) {
246-
s->len = new_len;
241+
if (new_len <= s->raw.len) {
242+
s->raw.len = new_len;
247243
} else {
248-
const size_t old_len = s->len;
244+
const size_t old_len = s->raw.len;
249245
#ifndef __clang_analyzer__
250246
// Clang-analyzer is smart enough to see the calloc(), but not smart
251247
// enough to link it to the free() in mstr_free()
252248
assert(new_len < SIZE_MAX);
253-
s->data = (char *)realloc((char *)s->data, new_len + 1);
249+
s->raw.data = (char *)realloc((char *)s->raw.data, new_len + 1);
254250
#endif
255-
s->len = new_len;
251+
s->raw.len = new_len;
256252
assert(new_len >= old_len);
257-
memset(s->data + old_len, 0, new_len - old_len);
253+
memset(s->raw.data + old_len, 0, new_len - old_len);
258254
}
259-
s->data[new_len] = (char)0;
255+
s->raw.data[new_len] = (char)0;
260256
}
261257

262258
/**
@@ -375,7 +371,7 @@ static inline mstr mstr_splice(mstr_view s, size_t at, size_t del_count, mstr_vi
375371
assert(s.len - del_count <= SIZE_MAX - insert.len);
376372
const size_t new_size = s.len - del_count + insert.len;
377373
mstr_mut ret = mstr_new(new_size);
378-
char *p = ret.data;
374+
char *p = ret.raw.data;
379375
memcpy(p, s.data, at);
380376
p += at;
381377
if (insert.data) {
@@ -457,7 +453,7 @@ static inline mstr mstr_substr(mstr_view s, size_t at, size_t len) {
457453
len = remain;
458454
}
459455
mstr_mut r = mstr_new(len);
460-
memcpy(r.data, s.data + at, len);
456+
memcpy(r.raw.data, s.data + at, len);
461457
return r.mstr;
462458
}
463459

@@ -613,7 +609,7 @@ static inline void _mstr_assert_(mstr_view left,
613609
if (!B) {
614610
mstr_assign(&pstr, mstr_prepend(pstr.view, mstrv_lit("not ")));
615611
}
616-
_mstr_assert_fail_(left, pstr.data, right, file, line);
612+
_mstr_assert_fail_(left, pstr.raw.data, right, file, line);
617613
}
618614
}
619615

@@ -773,9 +769,9 @@ static inline mstr_narrow_result mstr_win32_narrow(const wchar_t *wstring) {
773769
wcflags,
774770
wstring,
775771
-1,
776-
ret.data,
772+
ret.raw.data,
777773
// Plus one byte for the NUL
778-
(int)(ret.len + 1),
774+
(int)(ret.raw.len + 1),
779775
NULL,
780776
NULL);
781777
assert(length == got_len);

src/mlib/str.test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ int main(void) {
1212
(void)null_view;
1313

1414
str = mstr_copy_cstr("foo");
15-
CHECK(str.len == 3);
15+
CHECK(str.raw.len == 3);
1616
MSTR_ASSERT_EQ(str.view, mstrv_lit("foo"));
17-
CHECK(strncmp(str.data, "foo", 3) == 0);
17+
CHECK(strncmp(str.raw.data, "foo", 3) == 0);
1818

1919
mstr_inplace_append(&str, mstrv_lit("bar"));
2020
MSTR_ASSERT_EQ(str.view, mstrv_lit("foobar"));

src/mongocrypt-ciphertext-private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "mongocrypt.h"
2323

2424
/**
25-
* Produced by mongocrypt-marking.c from _mongocrypt_marking_t
25+
* Produced by mongocrypt-marking.u.fle1.c from _mongocrypt_marking_t
2626
* as encrypted payloads for blob_subtypes:
2727
* FLE1DeterministicEncryptedValue(1)
2828
* FLE1RandomEncryptedValue(2)

src/mongocrypt-ctx-encrypt.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,13 @@ static bool _collect_key_from_marking(void *ctx, _mongocrypt_buffer_t *in, mongo
281281
}
282282

283283
if (marking.type == MONGOCRYPT_MARKING_FLE1_BY_ID) {
284-
res = _mongocrypt_key_broker_request_id(kb, &marking.key_id);
284+
res = _mongocrypt_key_broker_request_id(kb, &marking.u.fle1.key_id);
285285
} else if (marking.type == MONGOCRYPT_MARKING_FLE1_BY_ALTNAME) {
286-
res = _mongocrypt_key_broker_request_name(kb, &marking.key_alt_name);
286+
res = _mongocrypt_key_broker_request_name(kb, &marking.u.fle1.key_alt_name);
287287
} else {
288288
BSON_ASSERT(marking.type == MONGOCRYPT_MARKING_FLE2_ENCRYPTION);
289-
res = _mongocrypt_key_broker_request_id(kb, &marking.fle2.index_key_id)
290-
&& _mongocrypt_key_broker_request_id(kb, &marking.fle2.user_key_id);
289+
res = _mongocrypt_key_broker_request_id(kb, &marking.u.fle2.index_key_id)
290+
&& _mongocrypt_key_broker_request_id(kb, &marking.u.fle2.user_key_id);
291291
}
292292

293293
if (!res) {
@@ -1318,23 +1318,23 @@ static bool _fle2_finalize_explicit(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *
13181318
}
13191319
// fallthrough
13201320
case MONGOCRYPT_QUERY_TYPE_RANGE:
1321-
case MONGOCRYPT_QUERY_TYPE_EQUALITY: marking.fle2.type = MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_FIND; break;
1321+
case MONGOCRYPT_QUERY_TYPE_EQUALITY: marking.u.fle2.type = MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_FIND; break;
13221322
default: _mongocrypt_ctx_fail_w_msg(ctx, "Invalid value for EncryptOpts.queryType"); goto fail;
13231323
}
13241324
} else {
1325-
marking.fle2.type = MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_INSERT;
1325+
marking.u.fle2.type = MONGOCRYPT_FLE2_PLACEHOLDER_TYPE_INSERT;
13261326
}
13271327

13281328
switch (ctx->opts.index_type.value) {
1329-
case MONGOCRYPT_INDEX_TYPE_EQUALITY: marking.fle2.algorithm = MONGOCRYPT_FLE2_ALGORITHM_EQUALITY; break;
1330-
case MONGOCRYPT_INDEX_TYPE_NONE: marking.fle2.algorithm = MONGOCRYPT_FLE2_ALGORITHM_UNINDEXED; break;
1329+
case MONGOCRYPT_INDEX_TYPE_EQUALITY: marking.u.fle2.algorithm = MONGOCRYPT_FLE2_ALGORITHM_EQUALITY; break;
1330+
case MONGOCRYPT_INDEX_TYPE_NONE: marking.u.fle2.algorithm = MONGOCRYPT_FLE2_ALGORITHM_UNINDEXED; break;
13311331
case MONGOCRYPT_INDEX_TYPE_RANGEPREVIEW_DEPRECATED:
13321332
if (ctx->crypt->opts.use_range_v2) {
13331333
_mongocrypt_ctx_fail_w_msg(ctx, "Cannot use rangePreview index type with Range V2");
13341334
goto fail;
13351335
}
13361336
// fallthrough
1337-
case MONGOCRYPT_INDEX_TYPE_RANGE: marking.fle2.algorithm = MONGOCRYPT_FLE2_ALGORITHM_RANGE; break;
1337+
case MONGOCRYPT_INDEX_TYPE_RANGE: marking.u.fle2.algorithm = MONGOCRYPT_FLE2_ALGORITHM_RANGE; break;
13381338
default:
13391339
// This might be unreachable because of other validation. Better safe than
13401340
// sorry.
@@ -1364,12 +1364,12 @@ static bool _fle2_finalize_explicit(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *
13641364
goto fail;
13651365
}
13661366

1367-
if (!bson_iter_init_find(&marking.v_iter, &new_v, "v")) {
1367+
if (!bson_iter_init_find(&marking.u.fle1.v_iter, &new_v, "v")) {
13681368
_mongocrypt_ctx_fail_w_msg(ctx, "invalid input BSON, must contain 'v'");
13691369
goto fail;
13701370
}
13711371

1372-
marking.fle2.sparsity = ctx->opts.rangeopts.value.sparsity;
1372+
marking.u.fle2.sparsity = ctx->opts.rangeopts.value.sparsity;
13731373

13741374
} else {
13751375
bson_t as_bson;
@@ -1380,21 +1380,21 @@ static bool _fle2_finalize_explicit(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *
13801380
goto fail;
13811381
}
13821382

1383-
if (!bson_iter_init_find(&marking.v_iter, &as_bson, "v")) {
1383+
if (!bson_iter_init_find(&marking.u.fle1.v_iter, &as_bson, "v")) {
13841384
_mongocrypt_ctx_fail_w_msg(ctx, "invalid input BSON, must contain 'v'");
13851385
goto fail;
13861386
}
13871387
}
13881388

1389-
_mongocrypt_buffer_copy_to(&ctx->opts.key_id, &marking.fle2.user_key_id);
1389+
_mongocrypt_buffer_copy_to(&ctx->opts.key_id, &marking.u.fle2.user_key_id);
13901390
if (!_mongocrypt_buffer_empty(&ctx->opts.index_key_id)) {
1391-
_mongocrypt_buffer_copy_to(&ctx->opts.index_key_id, &marking.fle2.index_key_id);
1391+
_mongocrypt_buffer_copy_to(&ctx->opts.index_key_id, &marking.u.fle2.index_key_id);
13921392
} else {
1393-
_mongocrypt_buffer_copy_to(&ctx->opts.key_id, &marking.fle2.index_key_id);
1393+
_mongocrypt_buffer_copy_to(&ctx->opts.key_id, &marking.u.fle2.index_key_id);
13941394
}
13951395

13961396
if (ctx->opts.contention_factor.set) {
1397-
marking.fle2.maxContentionFactor = ctx->opts.contention_factor.value;
1397+
marking.u.fle2.maxContentionFactor = ctx->opts.contention_factor.value;
13981398
} else if (ctx->opts.index_type.value == MONGOCRYPT_INDEX_TYPE_EQUALITY) {
13991399
_mongocrypt_ctx_fail_w_msg(ctx, "contention factor required for indexed algorithm");
14001400
goto fail;
@@ -1495,11 +1495,11 @@ static bool _finalize(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *out) {
14951495
return _mongocrypt_ctx_fail_w_msg(ctx, "invalid msg, must contain 'v'");
14961496
}
14971497

1498-
memcpy(&marking.v_iter, &iter, sizeof(bson_iter_t));
1499-
marking.algorithm = ctx->opts.algorithm;
1500-
_mongocrypt_buffer_set_to(&ctx->opts.key_id, &marking.key_id);
1498+
memcpy(&marking.u.fle1.v_iter, &iter, sizeof(bson_iter_t));
1499+
marking.u.fle1.algorithm = ctx->opts.algorithm;
1500+
_mongocrypt_buffer_set_to(&ctx->opts.key_id, &marking.u.fle1.key_id);
15011501
if (ctx->opts.key_alt_names) {
1502-
bson_value_copy(&ctx->opts.key_alt_names->value, &marking.key_alt_name);
1502+
bson_value_copy(&ctx->opts.key_alt_names->value, &marking.u.fle1.key_alt_name);
15031503
marking.type = MONGOCRYPT_MARKING_FLE1_BY_ALTNAME;
15041504
}
15051505

src/mongocrypt-marking-private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ typedef struct {
3939

4040
_mongocrypt_buffer_t key_id;
4141
bson_value_t key_alt_name;
42-
};
42+
} fle1;
4343

4444
mc_FLE2EncryptionPlaceholder_t fle2;
45-
};
45+
} u;
4646
} _mongocrypt_marking_t;
4747

4848
// `_mongocrypt_marking_t` inherits extended alignment from libbson. To dynamically allocate, use aligned allocation

0 commit comments

Comments
 (0)