Skip to content

Commit

Permalink
minor coverity fixes
Browse files Browse the repository at this point in the history
- assert that allocations succeed
- initialize structs containing enums with memset
  • Loading branch information
kevinAlbs committed Dec 9, 2019
1 parent c1850fa commit af44cc7
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions kms-message/src/kms_kv_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ kms_kv_list_add (kms_kv_list_t *lst,
if (lst->len == lst->size) {
lst->size *= 2;
lst->kvs = realloc (lst->kvs, lst->size * sizeof (kms_kv_t));
KMS_ASSERT (lst->kvs);
}

kv_init (&lst->kvs[lst->len], key, value);
Expand Down
1 change: 1 addition & 0 deletions kms-message/src/kms_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ kms_request_new (const char *method,
kms_request_t *request = calloc (1, sizeof (kms_request_t));
const char *question_mark;

KMS_ASSERT (request);
/* parsing may set failed to true */
request->failed = false;

Expand Down
1 change: 1 addition & 0 deletions kms-message/src/kms_response_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ _parser_init (kms_response_parser_t *parser)
parser->raw_response = kms_request_str_new ();
parser->content_length = -1;
parser->response = calloc (1, sizeof (kms_response_t));
KMS_ASSERT (parser->response);
parser->response->headers = kms_kv_list_new ();
parser->state = PARSING_STATUS_LINE;
parser->start = 0;
Expand Down
1 change: 1 addition & 0 deletions src/mongocrypt-binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mongocrypt_binary_new_from_data (uint8_t *data, uint32_t len)
mongocrypt_binary_t *binary;

binary = (mongocrypt_binary_t *) bson_malloc0 (sizeof *binary);
BSON_ASSERT (binary);
binary->data = data;
binary->len = len;

Expand Down
1 change: 1 addition & 0 deletions src/mongocrypt-ctx-datakey.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ mongocrypt_ctx_datakey_init (mongocrypt_ctx_t *ctx)
dkctx->encrypted_key_material.data =
bson_malloc (dkctx->encrypted_key_material.len);
dkctx->encrypted_key_material.owned = true;
BSON_ASSERT (dkctx->encrypted_key_material.data);

/* use a random IV. */
_mongocrypt_buffer_init (&iv);
Expand Down
2 changes: 1 addition & 1 deletion src/mongocrypt-ctx-decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ _finalize (mongocrypt_ctx_t *ctx, mongocrypt_binary_t *out)
bool res;

if (!ctx) {
return _mongocrypt_ctx_fail_w_msg (ctx, "null ctx");
return false;
}

if (!out) {
Expand Down
12 changes: 9 additions & 3 deletions src/mongocrypt-ctx-encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ _set_schema_from_collinfo (mongocrypt_ctx_t *ctx, bson_t *collinfo)

/* Disallow views. */
if (bson_iter_init_find (&iter, collinfo, "type") &&
BSON_ITER_HOLDS_UTF8 (&iter) &&
BSON_ITER_HOLDS_UTF8 (&iter) && bson_iter_utf8 (&iter, NULL) &&
0 == strcmp ("view", bson_iter_utf8 (&iter, NULL))) {
return _mongocrypt_ctx_fail_w_msg (ctx, "cannot auto encrypt a view");
}
Expand All @@ -64,7 +64,11 @@ _set_schema_from_collinfo (mongocrypt_ctx_t *ctx, bson_t *collinfo)
return _mongocrypt_ctx_fail_w_msg (ctx, "BSON malformed");
}
while (bson_iter_next (&iter)) {
if (0 == strcmp ("$jsonSchema", bson_iter_key (&iter))) {
const char* key;

key = bson_iter_key (&iter);
BSON_ASSERT (key);
if (0 == strcmp ("$jsonSchema", key)) {
if (found_jsonschema) {
return _mongocrypt_ctx_fail_w_msg (
ctx, "duplicate $jsonSchema fields found");
Expand Down Expand Up @@ -614,6 +618,7 @@ _check_cmd_for_auto_encrypt (mongocrypt_binary_t *cmd,
}

cmd_name = bson_iter_key (&iter);
BSON_ASSERT (cmd_name);

/* get the collection name (or NULL if database/client command). */
if (0 == strcmp (cmd_name, "explain")) {
Expand Down Expand Up @@ -745,9 +750,10 @@ mongocrypt_ctx_encrypt_init (mongocrypt_ctx_t *ctx,
mongocrypt_binary_t *cmd)
{
_mongocrypt_ctx_encrypt_t *ectx;
_mongocrypt_ctx_opts_spec_t opts_spec = {0};
_mongocrypt_ctx_opts_spec_t opts_spec;
bool bypass;

memset (&opts_spec, 0, sizeof (opts_spec));
opts_spec.schema = OPT_OPTIONAL;
if (!_mongocrypt_ctx_init (ctx, &opts_spec)) {
return false;
Expand Down
5 changes: 4 additions & 1 deletion src/mongocrypt-ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ mongocrypt_ctx_setopt_key_alt_name (mongocrypt_ctx_t *ctx,
bson_t as_bson;
bson_iter_t iter;
_mongocrypt_key_alt_name_t *new_key_alt_name;
const char* key;

if (!ctx) {
return false;
Expand All @@ -140,7 +141,9 @@ mongocrypt_ctx_setopt_key_alt_name (mongocrypt_ctx_t *ctx,
return _mongocrypt_ctx_fail_w_msg (ctx, "invalid bson");
}

if (0 != strcmp (bson_iter_key (&iter), "keyAltName")) {
key = bson_iter_key (&iter);
BSON_ASSERT (key);
if (0 != strcmp (key, "keyAltName")) {
return _mongocrypt_ctx_fail_w_msg (
ctx, "keyAltName must have field 'keyAltName'");
}
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 @@ -362,6 +362,7 @@ _mongocrypt_key_broker_filter (_mongocrypt_key_broker_t *kb,
char *key_str;

key_str = bson_strdup_printf ("%d", name_index++);
BSON_ASSERT (key_str);
if (!bson_append_value (&names,
key_str,
(uint32_t) strlen (key_str),
Expand Down
2 changes: 2 additions & 0 deletions src/mongocrypt-key.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ _parse_masterkey (bson_iter_t *iter,
const char *field;

field = bson_iter_key (&subiter);
BSON_ASSERT (field);
if (0 == strcmp ("provider", field)) {
const char *provider;

Expand All @@ -95,6 +96,7 @@ _parse_masterkey (bson_iter_t *iter,
return false;
}
provider = bson_iter_utf8 (&subiter, NULL);
BSON_ASSERT (provider);
if (0 == strcmp (provider, "aws")) {
out->masterkey_provider = MONGOCRYPT_KMS_PROVIDER_AWS;
} else if (0 == strcmp (provider, "local")) {
Expand Down
3 changes: 3 additions & 0 deletions src/mongocrypt-kms-ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ _mongocrypt_kms_ctx_init_aws_decrypt (mongocrypt_kms_ctx_t *kms,

/* create the KMS request. */
opt = kms_request_opt_new ();
BSON_ASSERT (opt);

_set_kms_crypto_hooks (crypto, opt);
/* TODO: we might want to let drivers control whether or not we send
Expand Down Expand Up @@ -261,6 +262,7 @@ _mongocrypt_kms_ctx_init_aws_encrypt (

/* create the KMS request. */
opt = kms_request_opt_new ();
BSON_ASSERT (opt);

_set_kms_crypto_hooks (crypto, opt);
/* TODO: we might want to let drivers control whether or not we send
Expand Down Expand Up @@ -450,6 +452,7 @@ mongocrypt_kms_ctx_feed (mongocrypt_kms_ctx_t *kms, mongocrypt_binary_t *bytes)
}

b64_str = (char *) bson_iter_utf8 (&iter, &b64_strlen);
BSON_ASSERT (b64_str);
kms->result.data = bson_malloc (b64_strlen + 1);
BSON_ASSERT (kms->result.data);

Expand Down
2 changes: 2 additions & 0 deletions src/mongocrypt-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ _mongocrypt_log (_mongocrypt_log_t *log,
message = bson_strdupv_printf (format, args);
va_end (args);

BSON_ASSERT (message);

_mongocrypt_mutex_lock (&log->mutex);
if (log->fn) {
log->fn (level, message, (uint32_t) strlen (message), log->ctx);
Expand Down
1 change: 1 addition & 0 deletions src/mongocrypt-marking.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ _mongocrypt_marking_parse_unowned (const _mongocrypt_buffer_t *in,
const char *field;

field = bson_iter_key (&iter);
BSON_ASSERT (field);
if (0 == strcmp ("ki", field)) {
has_ki = true;
if (!_mongocrypt_buffer_from_uuid_iter (&out->key_id, &iter)) {
Expand Down

0 comments on commit af44cc7

Please sign in to comment.