cipher: fix leak on initialization error
authorMarc-André Lureau <marcandre.lureau@redhat.com>
Wed, 9 Nov 2016 10:28:18 +0000 (14:28 +0400)
committerDaniel P. Berrange <berrange@redhat.com>
Wed, 21 Dec 2016 14:26:26 +0000 (14:26 +0000)
On error path, ctx may be leaked. Assign ctx earlier, and call
qcrypto_cipher_free() on error.

Spotted thanks to ASAN.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
crypto/cipher-nettle.c

index cd094cd6a564a596ecd85cb2b0c525c57841dc19..5798910d6c2e09d71a2675444db2dedb60f4636b 100644 (file)
@@ -254,6 +254,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
     cipher->mode = mode;
 
     ctx = g_new0(QCryptoCipherNettle, 1);
+    cipher->opaque = ctx;
 
     switch (alg) {
     case QCRYPTO_CIPHER_ALG_DES_RFB:
@@ -384,13 +385,11 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
     }
 
     ctx->iv = g_new0(uint8_t, ctx->blocksize);
-    cipher->opaque = ctx;
 
     return cipher;
 
  error:
-    g_free(cipher);
-    g_free(ctx);
+    qcrypto_cipher_free(cipher);
     return NULL;
 }