Skip to content

Commit ca0bb3a

Browse files
targosnodejs-github-bot
authored andcommitted
src: stop using v8::BackingStore::Reallocate
It's being deprecated by V8. Explicitly allocate a new ArrayBuffer and copy the data instead. Fixes: nodejs/node#52234
1 parent 1a4886b commit ca0bb3a

6 files changed

+54
-18
lines changed

src/crypto/crypto_cipher.cc

+21-8
Original file line numberDiff line numberDiff line change
@@ -820,10 +820,15 @@ CipherBase::UpdateResult CipherBase::Update(
820820
len);
821821

822822
CHECK_LE(static_cast<size_t>(buf_len), (*out)->ByteLength());
823-
if (buf_len == 0)
823+
if (buf_len == 0) {
824824
*out = ArrayBuffer::NewBackingStore(env()->isolate(), 0);
825-
else
826-
*out = BackingStore::Reallocate(env()->isolate(), std::move(*out), buf_len);
825+
} else {
826+
std::unique_ptr<BackingStore> old_out = std::move(*out);
827+
*out = ArrayBuffer::NewBackingStore(env()->isolate(), buf_len);
828+
memcpy(static_cast<char*>((*out)->Data()),
829+
static_cast<char*>(old_out->Data()),
830+
buf_len);
831+
}
827832

828833
// When in CCM mode, EVP_CipherUpdate will fail if the authentication tag is
829834
// invalid. In that case, remember the error and throw in final().
@@ -912,8 +917,11 @@ bool CipherBase::Final(std::unique_ptr<BackingStore>* out) {
912917

913918
CHECK_LE(static_cast<size_t>(out_len), (*out)->ByteLength());
914919
if (out_len > 0) {
915-
*out =
916-
BackingStore::Reallocate(env()->isolate(), std::move(*out), out_len);
920+
std::unique_ptr<BackingStore> old_out = std::move(*out);
921+
*out = ArrayBuffer::NewBackingStore(env()->isolate(), out_len);
922+
memcpy(static_cast<char*>((*out)->Data()),
923+
static_cast<char*>(old_out->Data()),
924+
out_len);
917925
} else {
918926
*out = ArrayBuffer::NewBackingStore(env()->isolate(), 0);
919927
}
@@ -1015,10 +1023,15 @@ bool PublicKeyCipher::Cipher(
10151023
}
10161024

10171025
CHECK_LE(out_len, (*out)->ByteLength());
1018-
if (out_len > 0)
1019-
*out = BackingStore::Reallocate(env->isolate(), std::move(*out), out_len);
1020-
else
1026+
if (out_len > 0) {
1027+
std::unique_ptr<BackingStore> old_out = std::move(*out);
1028+
*out = ArrayBuffer::NewBackingStore(env->isolate(), out_len);
1029+
memcpy(static_cast<char*>((*out)->Data()),
1030+
static_cast<char*>(old_out->Data()),
1031+
out_len);
1032+
} else {
10211033
*out = ArrayBuffer::NewBackingStore(env->isolate(), 0);
1034+
}
10221035

10231036
return true;
10241037
}

src/crypto/crypto_sig.cc

+8-3
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,15 @@ std::unique_ptr<BackingStore> Node_SignFinal(Environment* env,
9999
EVP_PKEY_sign(pkctx.get(), static_cast<unsigned char*>(sig->Data()),
100100
&sig_len, m, m_len)) {
101101
CHECK_LE(sig_len, sig->ByteLength());
102-
if (sig_len == 0)
102+
if (sig_len == 0) {
103103
sig = ArrayBuffer::NewBackingStore(env->isolate(), 0);
104-
else
105-
sig = BackingStore::Reallocate(env->isolate(), std::move(sig), sig_len);
104+
} else {
105+
std::unique_ptr<BackingStore> old_sig = std::move(sig);
106+
sig = ArrayBuffer::NewBackingStore(env->isolate(), sig_len);
107+
memcpy(static_cast<char*>(sig->Data()),
108+
static_cast<char*>(old_sig->Data()),
109+
sig_len);
110+
}
106111
return sig;
107112
}
108113

src/node_buffer.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,13 @@ MaybeLocal<Object> New(Isolate* isolate,
324324
CHECK(actual <= length);
325325

326326
if (LIKELY(actual > 0)) {
327-
if (actual < length)
328-
store = BackingStore::Reallocate(isolate, std::move(store), actual);
327+
if (actual < length) {
328+
std::unique_ptr<BackingStore> old_store = std::move(store);
329+
store = ArrayBuffer::NewBackingStore(isolate, actual);
330+
memcpy(static_cast<char*>(store->Data()),
331+
static_cast<char*>(old_store->Data()),
332+
actual);
333+
}
329334
Local<ArrayBuffer> buf = ArrayBuffer::New(isolate, std::move(store));
330335
Local<Object> obj;
331336
if (UNLIKELY(!New(isolate, buf, 0, actual).ToLocal(&obj)))

src/node_http2.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,11 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
20252025

20262026
if (LIKELY(stream_buf_offset_ == 0)) {
20272027
// Shrink to the actual amount of used data.
2028-
bs = BackingStore::Reallocate(env()->isolate(), std::move(bs), nread);
2028+
std::unique_ptr<BackingStore> old_bs = std::move(bs);
2029+
bs = ArrayBuffer::NewBackingStore(env()->isolate(), nread);
2030+
memcpy(static_cast<char*>(bs->Data()),
2031+
static_cast<char*>(old_bs->Data()),
2032+
nread);
20292033
} else {
20302034
// This is a very unlikely case, and should only happen if the ReadStart()
20312035
// call in OnStreamAfterWrite() immediately provides data. If that does

src/stream_base.cc

+8-3
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,14 @@ void EmitToJSStreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
679679
}
680680

681681
CHECK_LE(static_cast<size_t>(nread), bs->ByteLength());
682-
bs = BackingStore::Reallocate(isolate, std::move(bs), nread);
683-
684-
stream->CallJSOnreadMethod(nread, ArrayBuffer::New(isolate, std::move(bs)));
682+
std::unique_ptr<BackingStore> new_bs =
683+
ArrayBuffer::NewBackingStore(isolate, nread);
684+
memcpy(static_cast<char*>(new_bs->Data()),
685+
static_cast<char*>(bs->Data()),
686+
nread);
687+
688+
stream->CallJSOnreadMethod(nread,
689+
ArrayBuffer::New(isolate, std::move(new_bs)));
685690
}
686691

687692

src/udp_wrap.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,11 @@ void UDPWrap::OnRecv(ssize_t nread,
766766
bs = ArrayBuffer::NewBackingStore(isolate, 0);
767767
} else {
768768
CHECK_LE(static_cast<size_t>(nread), bs->ByteLength());
769-
bs = BackingStore::Reallocate(isolate, std::move(bs), nread);
769+
std::unique_ptr<BackingStore> old_bs = std::move(bs);
770+
bs = ArrayBuffer::NewBackingStore(isolate, nread);
771+
memcpy(static_cast<char*>(bs->Data()),
772+
static_cast<char*>(old_bs->Data()),
773+
nread);
770774
}
771775

772776
Local<Object> address;

0 commit comments

Comments
 (0)