Skip to content

Commit

Permalink
MONGOCRYPT-726 Remove/replace unnecessary memset calls. (#892)
Browse files Browse the repository at this point in the history
* remove unnecessary memset

* prefer `_mongocrypt_buffer_init` over memset

* prefer zero-init over memset

* prefer compound literal to memset

* document purpose of memset call
  • Loading branch information
kevinAlbs authored Oct 8, 2024
1 parent 7aeaec4 commit 4e99f6e
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/mongocrypt-ciphertext.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ bool _mongocrypt_ciphertext_parse_unowned(_mongocrypt_buffer_t *in,
ciphertext->original_bson_type = in->data[offset];
offset += 1;

memset(&ciphertext->data, 0, sizeof(ciphertext->data));
_mongocrypt_buffer_init(&ciphertext->data);
ciphertext->data.data = in->data + offset;
ciphertext->data.len = in->len - offset;

Expand Down
6 changes: 2 additions & 4 deletions src/mongocrypt-ctx-encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1214,14 +1214,12 @@ _marking_to_bson_value(void *ctx, _mongocrypt_marking_t *marking, bson_value_t *

static bool
_replace_marking_with_ciphertext(void *ctx, _mongocrypt_buffer_t *in, bson_value_t *out, mongocrypt_status_t *status) {
_mongocrypt_marking_t marking;
_mongocrypt_marking_t marking = {0};
bool ret;

BSON_ASSERT_PARAM(ctx);
BSON_ASSERT_PARAM(in);

memset(&marking, 0, sizeof(marking));

if (!_mongocrypt_marking_parse_unowned(in, &marking, status)) {
_mongocrypt_marking_cleanup(&marking);
return false;
Expand Down Expand Up @@ -2425,7 +2423,7 @@ static bool explicit_encrypt_init(mongocrypt_ctx_t *ctx, mongocrypt_binary_t *ms
_mongocrypt_ctx_encrypt_t *ectx;
bson_t as_bson;
bson_iter_t iter;
_mongocrypt_ctx_opts_spec_t opts_spec;
_mongocrypt_ctx_opts_spec_t opts_spec = {0};

if (!ctx) {
return false;
Expand Down
1 change: 1 addition & 0 deletions src/mongocrypt-key-broker.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@ void _mongocrypt_key_broker_add_test_key(_mongocrypt_key_broker_t *kb, const _mo
key_returned->decrypted = true;
_mongocrypt_buffer_init(&key_returned->decrypted_key_material);
_mongocrypt_buffer_resize(&key_returned->decrypted_key_material, MONGOCRYPT_KEY_LEN);
// Initialize test key material with all zeros.
memset(key_returned->decrypted_key_material.data, 0, MONGOCRYPT_KEY_LEN);
_mongocrypt_key_destroy(key_doc);
/* Hijack state and move directly to DONE. */
Expand Down
1 change: 0 additions & 1 deletion src/mongocrypt-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ void _mongocrypt_log_cleanup(_mongocrypt_log_t *log) {
}

_mongocrypt_mutex_cleanup(&log->mutex);
memset(log, 0, sizeof(*log));
}

void _mongocrypt_stdout_log_fn(mongocrypt_log_level_t level, const char *message, uint32_t message_len, void *ctx) {
Expand Down
3 changes: 2 additions & 1 deletion src/mongocrypt-marking.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ static void _FLE2EncryptedPayloadCommon_cleanup(_FLE2EncryptedPayloadCommon_t *c
_mongocrypt_buffer_cleanup(&common->escDerivedToken);
_mongocrypt_buffer_cleanup(&common->eccDerivedToken);
_mongocrypt_buffer_cleanup(&common->serverDerivedFromDataToken);
memset(common, 0, sizeof(*common));
// Zero out memory so `_FLE2EncryptedPayloadCommon_cleanup` is safe to call twice.
*common = (_FLE2EncryptedPayloadCommon_t){{0}};
}

// _get_tokenKey returns the tokenKey identified by indexKeyId.
Expand Down

0 comments on commit 4e99f6e

Please sign in to comment.