From: Aki Tuomi Date: Wed, 14 Dec 2016 08:23:17 +0000 (+0200) Subject: mail-crypt: Ensure array is created before accessing it X-Git-Tag: 2.3.0.rc1~2442 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e41e5d5456fb6c78693a385239ef5b0cbbd57408;p=thirdparty%2Fdovecot%2Fcore.git mail-crypt: Ensure array is created before accessing it Fixes segmentation fault on fs-crypt when keys are not configured. --- diff --git a/src/plugins/mail-crypt/mail-crypt-global-key.c b/src/plugins/mail-crypt/mail-crypt-global-key.c index f31c901174..7ba47063e1 100644 --- a/src/plugins/mail-crypt/mail-crypt-global-key.c +++ b/src/plugins/mail-crypt/mail-crypt-global-key.c @@ -161,6 +161,9 @@ mail_crypt_global_key_find(struct mail_crypt_global_keys *global_keys, { const struct mail_crypt_global_private_key *priv_key; + if (!array_is_created(&global_keys->private_keys)) + return NULL; + array_foreach(&global_keys->private_keys, priv_key) { if (strcmp(priv_key->key_id, pubkey_digest) == 0) return priv_key->key; diff --git a/src/plugins/mail-crypt/test-mail-global-key.c b/src/plugins/mail-crypt/test-mail-global-key.c index 2fc8eac992..c1f04bb515 100644 --- a/src/plugins/mail-crypt/test-mail-global-key.c +++ b/src/plugins/mail-crypt/test-mail-global-key.c @@ -94,6 +94,18 @@ static void test_try_load_keys(void) test_end(); } +static void test_empty_keyset(void) +{ + test_begin("test_empty_keyset"); + + /* this should not crash */ + struct mail_crypt_global_keys keys; + memset(&keys, 0, sizeof(keys)); + test_assert(mail_crypt_global_key_find(&keys, "423423423423") == NULL); + + test_end(); +} + static void test_teardown(void) { array_free(&fs_set.plugin_envs); @@ -105,6 +117,7 @@ int main(void) void (*tests[])(void) = { test_setup, test_try_load_keys, + test_empty_keyset, test_teardown, NULL };