From 77f21787d484fe287083a47750e9533bd362ddbc Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 13 Dec 2011 21:22:15 +0100 Subject: [PATCH] crypto: fix memory leak when decrypting empty strings Also fixes a dangling pointer delete[] in the error path. --- src/node_crypto.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index c4754dbe984..b71d7cded42 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -2327,7 +2327,12 @@ class Decipher : public ObjectWrap { } int DecipherUpdate(char* data, int len, unsigned char** out, int* out_len) { - if (!initialised_) return 0; + if (!initialised_) { + *out_len = 0; + *out = NULL; + return 0; + } + *out_len=len+EVP_CIPHER_CTX_block_size(&ctx); *out= new unsigned char[*out_len]; @@ -2337,7 +2342,12 @@ class Decipher : public ObjectWrap { // coverity[alloc_arg] int DecipherFinal(unsigned char** out, int *out_len, bool tolerate_padding) { - if (!initialised_) return 0; + if (!initialised_) { + *out_len = 0; + *out = NULL; + return 0; + } + *out = new unsigned char[EVP_CIPHER_CTX_block_size(&ctx)]; if (tolerate_padding) { local_EVP_DecryptFinal_ex(&ctx,*out,out_len); @@ -2597,10 +2607,10 @@ class Decipher : public ObjectWrap { int r = cipher->DecipherFinal(&out_value, &out_len, false); if (out_len == 0 || r == 0) { + delete[] out_value; return scope.Close(String::New("")); } - if (args.Length() == 0 || !args[0]->IsString()) { outString = Encode(out_value, out_len, BINARY); } else {