Skip to content

Commit 2ee31aa

Browse files
bnoordhuisaddaleax
authored andcommitted
src: remove extra heap allocations in CipherBase
Don't allocate + copy + free; allocate and fill in place, then hand off the pointer to Buffer::New(). Avoids unnecessary heap allocations in the following methods: - crypto.Cipher#final() - crypto.Cipher#update() - crypto.Cipheriv#final() - crypto.Cipheriv#update() - crypto.Decipher#final() - crypto.Decipher#update() - crypto.Decipheriv#final() - crypto.Decipheriv#update() - crypto.privateDecrypt() - crypto.privateEncrypt() - crypto.publicDecrypt() - crypto.publicEncrypt() PR-URL: #14122 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 1444601 commit 2ee31aa

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/node_crypto.cc

+12-16
Original file line numberDiff line numberDiff line change
@@ -3561,7 +3561,7 @@ bool CipherBase::Update(const char* data,
35613561
}
35623562

35633563
*out_len = len + EVP_CIPHER_CTX_block_size(&ctx_);
3564-
*out = new unsigned char[*out_len];
3564+
*out = Malloc<unsigned char>(static_cast<size_t>(*out_len));
35653565
return EVP_CipherUpdate(&ctx_,
35663566
*out,
35673567
out_len,
@@ -3595,17 +3595,15 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
35953595
}
35963596

35973597
if (!r) {
3598-
delete[] out;
3598+
free(out);
35993599
return ThrowCryptoError(env,
36003600
ERR_get_error(),
36013601
"Trying to add data in unsupported state");
36023602
}
36033603

36043604
CHECK(out != nullptr || out_len == 0);
36053605
Local<Object> buf =
3606-
Buffer::Copy(env, reinterpret_cast<char*>(out), out_len).ToLocalChecked();
3607-
if (out)
3608-
delete[] out;
3606+
Buffer::New(env, reinterpret_cast<char*>(out), out_len).ToLocalChecked();
36093607

36103608
args.GetReturnValue().Set(buf);
36113609
}
@@ -3633,7 +3631,8 @@ bool CipherBase::Final(unsigned char** out, int *out_len) {
36333631
if (!initialised_)
36343632
return false;
36353633

3636-
*out = new unsigned char[EVP_CIPHER_CTX_block_size(&ctx_)];
3634+
*out = Malloc<unsigned char>(
3635+
static_cast<size_t>(EVP_CIPHER_CTX_block_size(&ctx_)));
36373636
int r = EVP_CipherFinal_ex(&ctx_, *out, out_len);
36383637

36393638
if (r && kind_ == kCipher) {
@@ -3670,7 +3669,7 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
36703669
bool r = cipher->Final(&out_value, &out_len);
36713670

36723671
if (out_len <= 0 || !r) {
3673-
delete[] out_value;
3672+
free(out_value);
36743673
out_value = nullptr;
36753674
out_len = 0;
36763675
if (!r) {
@@ -3684,12 +3683,11 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
36843683
}
36853684
}
36863685

3687-
Local<Object> buf = Buffer::Copy(
3686+
Local<Object> buf = Buffer::New(
36883687
env,
36893688
reinterpret_cast<char*>(out_value),
36903689
out_len).ToLocalChecked();
36913690
args.GetReturnValue().Set(buf);
3692-
delete[] out_value;
36933691
}
36943692

36953693

@@ -4560,7 +4558,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
45604558
if (EVP_PKEY_cipher(ctx, nullptr, out_len, data, len) <= 0)
45614559
goto exit;
45624560

4563-
*out = new unsigned char[*out_len];
4561+
*out = Malloc<unsigned char>(*out_len);
45644562

45654563
if (EVP_PKEY_cipher(ctx, *out, out_len, data, len) <= 0)
45664564
goto exit;
@@ -4615,7 +4613,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
46154613
&out_len);
46164614

46174615
if (out_len == 0 || !r) {
4618-
delete[] out_value;
4616+
free(out_value);
46194617
out_value = nullptr;
46204618
out_len = 0;
46214619
if (!r) {
@@ -4624,12 +4622,10 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
46244622
}
46254623
}
46264624

4627-
Local<Object> vbuf = Buffer::Copy(
4628-
env,
4629-
reinterpret_cast<char*>(out_value),
4630-
out_len).ToLocalChecked();
4625+
Local<Object> vbuf =
4626+
Buffer::New(env, reinterpret_cast<char*>(out_value), out_len)
4627+
.ToLocalChecked();
46314628
args.GetReturnValue().Set(vbuf);
4632-
delete[] out_value;
46334629
}
46344630

46354631

0 commit comments

Comments
 (0)