summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaiki Ueno <ueno@gnu.org>2020-06-02 05:34:29 +0200
committerDaiki Ueno <ueno@gnu.org>2020-08-31 08:06:03 +0200
commitb875af8d74fef8fd63704628c4818546765492fd (patch)
tree14c864c8ee7c8bb391dac91681e29414d40781b3
parent0e131a654f9a5bf97fae21236e4e30320f360fd6 (diff)
downloadgnutls-b875af8d74fef8fd63704628c4818546765492fd.tar.gz
gnutls_aead_cipher_init: fix potential memleak
When _gnutls_aead_cipher_init() fails, the function returns without freeing the allocted handle. This was once fixed in commit 502be130493e8ce802cdf60fffdbb5f1885352a5 but regressed after a code reorganization in commit 2eef509ce5f2d250f8dcaeffa46444dd2b694e91. Reported by Miroslav Lichvar. Signed-off-by: Daiki Ueno <ueno@gnu.org>
-rw-r--r--lib/crypto-api.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/crypto-api.c b/lib/crypto-api.c
index 8524f5ed4f..f289ebcd03 100644
--- a/lib/crypto-api.c
+++ b/lib/crypto-api.c
@@ -755,6 +755,7 @@ int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t *handle,
{
api_aead_cipher_hd_st *h;
const cipher_entry_st *e;
+ int ret;
if (is_cipher_algo_forbidden(cipher))
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
@@ -763,15 +764,21 @@ int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t *handle,
if (e == NULL || e->type != CIPHER_AEAD)
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
- *handle = gnutls_calloc(1, sizeof(api_aead_cipher_hd_st));
- if (*handle == NULL) {
+ h = gnutls_calloc(1, sizeof(api_aead_cipher_hd_st));
+ if (h == NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
- h = *handle;
+ ret = _gnutls_aead_cipher_init(h, cipher, key);
+ if (ret < 0) {
+ gnutls_free(h);
+ return ret;
+ }
- return _gnutls_aead_cipher_init(h, cipher, key);
+ *handle = h;
+
+ return ret;
}
/**