@@ -1890,7 +1890,8 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
18901890 int slen = i2d_SSL_SESSION (sess, nullptr );
18911891 CHECK_GT (slen, 0 );
18921892
1893- char * sbuf = Malloc (slen);
1893+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
1894+ char * sbuf = static_cast <char *>(allocator->AllocateUninitialized (slen));
18941895 unsigned char * p = reinterpret_cast <unsigned char *>(sbuf);
18951896 i2d_SSL_SESSION (sess, &p);
18961897 args.GetReturnValue ().Set (Buffer::New (env, sbuf, slen).ToLocalChecked ());
@@ -3011,7 +3012,8 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
30113012 return kErrorState ;
30123013 }
30133014
3014- *out = Malloc<unsigned char >(buff_len);
3015+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3016+ *out = static_cast <unsigned char *>(allocator->AllocateUninitialized (buff_len));
30153017 int r = EVP_CipherUpdate (ctx_.get (),
30163018 *out,
30173019 out_len,
@@ -3053,7 +3055,8 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
30533055 }
30543056
30553057 if (r != kSuccess ) {
3056- free (out);
3058+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3059+ allocator->Free (out, out_len);
30573060 if (r == kErrorState ) {
30583061 ThrowCryptoError (env, ERR_get_error (),
30593062 " Trying to add data in unsupported state" );
@@ -3091,8 +3094,9 @@ bool CipherBase::Final(unsigned char** out, int* out_len) {
30913094
30923095 const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
30933096
3094- *out = Malloc<unsigned char >(
3095- static_cast <size_t >(EVP_CIPHER_CTX_block_size (ctx_.get ())));
3097+ auto * allocator = env ()->isolate ()->GetArrayBufferAllocator ();
3098+ *out = static_cast <unsigned char *>(allocator->AllocateUninitialized (
3099+ EVP_CIPHER_CTX_block_size (ctx_.get ())));
30963100
30973101 // In CCM mode, final() only checks whether authentication failed in update().
30983102 // EVP_CipherFinal_ex must not be called and will fail.
@@ -3135,7 +3139,8 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
31353139 bool r = cipher->Final (&out_value, &out_len);
31363140
31373141 if (out_len <= 0 || !r) {
3138- free (out_value);
3142+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3143+ allocator->Free (out_value, out_len);
31393144 out_value = nullptr ;
31403145 out_len = 0 ;
31413146 if (!r) {
@@ -3781,7 +3786,8 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
37813786template <PublicKeyCipher::Operation operation,
37823787 PublicKeyCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
37833788 PublicKeyCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
3784- bool PublicKeyCipher::Cipher (const char * key_pem,
3789+ bool PublicKeyCipher::Cipher (Environment* env,
3790+ const char * key_pem,
37853791 int key_pem_len,
37863792 const char * passphrase,
37873793 int padding,
@@ -3790,6 +3796,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
37903796 unsigned char ** out,
37913797 size_t * out_len) {
37923798 EVPKeyPointer pkey;
3799+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
37933800
37943801 BIOPointer bp (BIO_new_mem_buf (const_cast <char *>(key_pem), key_pem_len));
37953802 if (!bp)
@@ -3837,7 +3844,7 @@ bool PublicKeyCipher::Cipher(const char* key_pem,
38373844 if (EVP_PKEY_cipher (ctx.get (), nullptr , out_len, data, len) <= 0 )
38383845 return false ;
38393846
3840- *out = Malloc <unsigned char >( *out_len);
3847+ *out = static_cast <unsigned char *>(allocator-> AllocateUninitialized ( *out_len) );
38413848
38423849 if (EVP_PKEY_cipher (ctx.get (), *out, out_len, data, len) <= 0 )
38433850 return false ;
@@ -3870,6 +3877,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
38703877 ClearErrorOnReturn clear_error_on_return;
38713878
38723879 bool r = Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
3880+ env,
38733881 kbuf,
38743882 klen,
38753883 args.Length () >= 3 && !args[2 ]->IsNull () ? *passphrase : nullptr ,
@@ -3880,7 +3888,8 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
38803888 &out_len);
38813889
38823890 if (out_len == 0 || !r) {
3883- free (out_value);
3891+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
3892+ allocator->Free (out_value, out_len);
38843893 out_value = nullptr ;
38853894 out_len = 0 ;
38863895 if (!r) {
@@ -4085,7 +4094,8 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
40854094 const BIGNUM* pub_key;
40864095 DH_get0_key (diffieHellman->dh_ .get (), &pub_key, nullptr );
40874096 size_t size = BN_num_bytes (pub_key);
4088- char * data = Malloc (size);
4097+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4098+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
40894099 BN_bn2bin (pub_key, reinterpret_cast <unsigned char *>(data));
40904100 args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
40914101}
@@ -4104,7 +4114,8 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
41044114 if (num == nullptr ) return env->ThrowError (err_if_null);
41054115
41064116 size_t size = BN_num_bytes (num);
4107- char * data = Malloc (size);
4117+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4118+ char * data = static_cast <char *>(allocator->AllocateUninitialized (size));
41084119 BN_bn2bin (num, reinterpret_cast <unsigned char *>(data));
41094120 args.GetReturnValue ().Set (Buffer::New (env, data, size).ToLocalChecked ());
41104121}
@@ -4168,7 +4179,8 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
41684179 Buffer::Length (args[0 ]),
41694180 0 ));
41704181
4171- MallocedBuffer<char > data (DH_size (diffieHellman->dh_ .get ()));
4182+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4183+ MallocedBuffer<char > data (DH_size (diffieHellman->dh_ .get ()), allocator);
41724184
41734185 int size = DH_compute_key (reinterpret_cast <unsigned char *>(data.data ),
41744186 key.get (),
@@ -4388,13 +4400,14 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
43884400 }
43894401
43904402 // NOTE: field_size is in bits
4403+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
43914404 int field_size = EC_GROUP_get_degree (ecdh->group_ );
43924405 size_t out_len = (field_size + 7 ) / 8 ;
4393- char * out = node::Malloc ( out_len);
4406+ char * out = static_cast < char *>(allocator-> AllocateUninitialized ( out_len) );
43944407
43954408 int r = ECDH_compute_key (out, out_len, pub.get (), ecdh->key_ .get (), nullptr );
43964409 if (!r) {
4397- free (out);
4410+ allocator-> Free (out, out_len );
43984411 return env->ThrowError (" Failed to compute ECDH key" );
43994412 }
44004413
@@ -4424,11 +4437,13 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
44244437 if (size == 0 )
44254438 return env->ThrowError (" Failed to get public key length" );
44264439
4427- unsigned char * out = node::Malloc<unsigned char >(size);
4440+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
4441+ unsigned char * out =
4442+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
44284443
44294444 int r = EC_POINT_point2oct (ecdh->group_ , pub, form, out, size, nullptr );
44304445 if (r != size) {
4431- free (out);
4446+ allocator-> Free (out, size );
44324447 return env->ThrowError (" Failed to get public key" );
44334448 }
44344449
@@ -4448,11 +4463,13 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
44484463 if (b == nullptr )
44494464 return env->ThrowError (" Failed to get ECDH private key" );
44504465
4466+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
44514467 int size = BN_num_bytes (b);
4452- unsigned char * out = node::Malloc<unsigned char >(size);
4468+ unsigned char * out =
4469+ static_cast <unsigned char *>(allocator->AllocateUninitialized (size));
44534470
44544471 if (size != BN_bn2bin (b, out)) {
4455- free (out);
4472+ allocator-> Free (out, size );
44564473 return env->ThrowError (" Failed to convert ECDH private key to Buffer" );
44574474 }
44584475
@@ -4572,7 +4589,7 @@ class PBKDF2Request : public AsyncWrap, public ThreadPoolWork {
45724589 success_(false ),
45734590 pass_(std::move(pass)),
45744591 salt_(std::move(salt)),
4575- key_(keylen),
4592+ key_(keylen, env-> isolate ()->GetArrayBufferAllocator() ),
45764593 iteration_count_(iteration_count) {
45774594 }
45784595
@@ -4634,6 +4651,7 @@ void PBKDF2Request::AfterThreadPoolWork(int status) {
46344651
46354652void PBKDF2 (const FunctionCallbackInfo<Value>& args) {
46364653 Environment* env = Environment::GetCurrent (args);
4654+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
46374655
46384656 const EVP_MD* digest = nullptr ;
46394657 int keylen = -1 ;
@@ -4642,12 +4660,12 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
46424660
46434661 int passlen = Buffer::Length (args[0 ]);
46444662
4645- MallocedBuffer<char > pass (passlen);
4663+ MallocedBuffer<char > pass (passlen, allocator );
46464664 memcpy (pass.data , Buffer::Data (args[0 ]), passlen);
46474665
46484666 int saltlen = Buffer::Length (args[1 ]);
46494667
4650- MallocedBuffer<char > salt (saltlen);
4668+ MallocedBuffer<char > salt (saltlen, allocator );
46514669 memcpy (salt.data , Buffer::Data (args[1 ]), saltlen);
46524670
46534671 iteration_count = args[2 ]->Int32Value (env->context ()).FromJust ();
@@ -4724,9 +4742,10 @@ class RandomBytesRequest : public AsyncWrap, public ThreadPoolWork {
47244742 }
47254743
47264744 inline void release () {
4745+ size_t free_size = size_;
47274746 size_ = 0 ;
47284747 if (free_mode_ == FREE_DATA) {
4729- free ( data_);
4748+ env ()-> isolate ()-> GetArrayBufferAllocator ()-> Free ( data_, free_size );
47304749 data_ = nullptr ;
47314750 }
47324751 }
@@ -4840,7 +4859,8 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
48404859
48414860 Local<Object> obj = env->randombytes_constructor_template ()->
48424861 NewInstance (env->context ()).ToLocalChecked ();
4843- char * data = node::Malloc (size);
4862+ char * data = static_cast <char *>(
4863+ env->isolate ()->GetArrayBufferAllocator ()->AllocateUninitialized (size));
48444864 std::unique_ptr<RandomBytesRequest> req (
48454865 new RandomBytesRequest (env,
48464866 obj,
@@ -5015,8 +5035,9 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
50155035}
50165036
50175037
5018- char * ExportPublicKey (const char * data, int len, size_t * size) {
5038+ char * ExportPublicKey (Environment* env, const char * data, int len, size_t * size) {
50195039 char * buf = nullptr ;
5040+ auto * allocator = env->isolate ()->GetArrayBufferAllocator ();
50205041
50215042 BIOPointer bio (BIO_new (BIO_s_mem ()));
50225043 if (!bio)
@@ -5037,7 +5058,7 @@ char* ExportPublicKey(const char* data, int len, size_t* size) {
50375058 BIO_get_mem_ptr (bio.get (), &ptr);
50385059
50395060 *size = ptr->length ;
5040- buf = Malloc ( *size);
5061+ buf = static_cast < char *>(allocator-> AllocateUninitialized ( *size) );
50415062 memcpy (buf, ptr->data , *size);
50425063
50435064 return buf;
@@ -5055,7 +5076,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
50555076 CHECK_NE (data, nullptr );
50565077
50575078 size_t pkey_size;
5058- char * pkey = ExportPublicKey (data, length, &pkey_size);
5079+ char * pkey = ExportPublicKey (env, data, length, &pkey_size);
50595080 if (pkey == nullptr )
50605081 return args.GetReturnValue ().SetEmptyString ();
50615082
0 commit comments