From 57b9f113fce2a2231a47e9295c1d461e9ff7f0f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Tue, 23 Jul 2019 16:22:36 +0100 Subject: crypto: use auto cleanup for many stack variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify cleanup paths by using glib's auto cleanup macros for stack variables, allowing several goto jumps / labels to be eliminated. Reviewed-by: Marc-André Lureau Reviewed-by: Stefan Hajnoczi Signed-off-by: Daniel P. Berrangé --- crypto/afsplit.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) (limited to 'crypto/afsplit.c') diff --git a/crypto/afsplit.c b/crypto/afsplit.c index 328d68c96b..b1a5a20899 100644 --- a/crypto/afsplit.c +++ b/crypto/afsplit.c @@ -58,7 +58,7 @@ static int qcrypto_afsplit_hash(QCryptoHashAlgorithm hash, } for (i = 0; i < hashcount; i++) { - uint8_t *out = NULL; + g_autofree uint8_t *out = NULL; size_t outlen = 0; uint32_t iv = cpu_to_be32(i); struct iovec in[] = { @@ -79,7 +79,6 @@ static int qcrypto_afsplit_hash(QCryptoHashAlgorithm hash, assert(outlen == digestlen); memcpy(block + (i * digestlen), out, (i == (hashcount - 1)) ? finallen : digestlen); - g_free(out); } return 0; @@ -93,13 +92,12 @@ int qcrypto_afsplit_encode(QCryptoHashAlgorithm hash, uint8_t *out, Error **errp) { - uint8_t *block = g_new0(uint8_t, blocklen); + g_autofree uint8_t *block = g_new0(uint8_t, blocklen); size_t i; - int ret = -1; for (i = 0; i < (stripes - 1); i++) { if (qcrypto_random_bytes(out + (i * blocklen), blocklen, errp) < 0) { - goto cleanup; + return -1; } qcrypto_afsplit_xor(blocklen, @@ -108,18 +106,14 @@ int qcrypto_afsplit_encode(QCryptoHashAlgorithm hash, block); if (qcrypto_afsplit_hash(hash, blocklen, block, errp) < 0) { - goto cleanup; + return -1; } } qcrypto_afsplit_xor(blocklen, in, block, out + (i * blocklen)); - ret = 0; - - cleanup: - g_free(block); - return ret; + return 0; } @@ -130,9 +124,8 @@ int qcrypto_afsplit_decode(QCryptoHashAlgorithm hash, uint8_t *out, Error **errp) { - uint8_t *block = g_new0(uint8_t, blocklen); + g_autofree uint8_t *block = g_new0(uint8_t, blocklen); size_t i; - int ret = -1; for (i = 0; i < (stripes - 1); i++) { qcrypto_afsplit_xor(blocklen, @@ -141,7 +134,7 @@ int qcrypto_afsplit_decode(QCryptoHashAlgorithm hash, block); if (qcrypto_afsplit_hash(hash, blocklen, block, errp) < 0) { - goto cleanup; + return -1; } } @@ -149,10 +142,5 @@ int qcrypto_afsplit_decode(QCryptoHashAlgorithm hash, in + (i * blocklen), block, out); - - ret = 0; - - cleanup: - g_free(block); - return ret; + return 0; } -- cgit v1.2.3