Skip to content

Commit 7e35a16

Browse files
targosjoyeecheung
authored andcommittedMay 3, 2024
src: stop using v8::BackingStore::Reallocate
It's being deprecated by V8. Explicitly allocate a new ArrayBuffer and copy the data when needed instead. Fixes: #52234 Co-authored-by: Joyee Cheung <joyeec9h3@gmail.com> PR-URL: #52292 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 0f690f0 commit 7e35a16

File tree

6 files changed

+58
-20
lines changed

6 files changed

+58
-20
lines changed
 

‎src/crypto/crypto_cipher.cc

+23-10
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,15 @@ CipherBase::UpdateResult CipherBase::Update(
830830
len);
831831

832832
CHECK_LE(static_cast<size_t>(buf_len), (*out)->ByteLength());
833-
if (buf_len == 0)
833+
if (buf_len == 0) {
834834
*out = ArrayBuffer::NewBackingStore(env()->isolate(), 0);
835-
else
836-
*out = BackingStore::Reallocate(env()->isolate(), std::move(*out), buf_len);
835+
} else if (static_cast<size_t>(buf_len) != (*out)->ByteLength()) {
836+
std::unique_ptr<BackingStore> old_out = std::move(*out);
837+
*out = ArrayBuffer::NewBackingStore(env()->isolate(), buf_len);
838+
memcpy(static_cast<char*>((*out)->Data()),
839+
static_cast<char*>(old_out->Data()),
840+
buf_len);
841+
}
837842

838843
// When in CCM mode, EVP_CipherUpdate will fail if the authentication tag is
839844
// invalid. In that case, remember the error and throw in final().
@@ -921,11 +926,14 @@ bool CipherBase::Final(std::unique_ptr<BackingStore>* out) {
921926
&out_len) == 1;
922927

923928
CHECK_LE(static_cast<size_t>(out_len), (*out)->ByteLength());
924-
if (out_len > 0) {
925-
*out =
926-
BackingStore::Reallocate(env()->isolate(), std::move(*out), out_len);
927-
} else {
929+
if (out_len == 0) {
928930
*out = ArrayBuffer::NewBackingStore(env()->isolate(), 0);
931+
} else if (static_cast<size_t>(out_len) != (*out)->ByteLength()) {
932+
std::unique_ptr<BackingStore> old_out = std::move(*out);
933+
*out = ArrayBuffer::NewBackingStore(env()->isolate(), out_len);
934+
memcpy(static_cast<char*>((*out)->Data()),
935+
static_cast<char*>(old_out->Data()),
936+
out_len);
929937
}
930938

931939
if (ok && kind_ == kCipher && IsAuthenticatedMode()) {
@@ -1025,10 +1033,15 @@ bool PublicKeyCipher::Cipher(
10251033
}
10261034

10271035
CHECK_LE(out_len, (*out)->ByteLength());
1028-
if (out_len > 0)
1029-
*out = BackingStore::Reallocate(env->isolate(), std::move(*out), out_len);
1030-
else
1036+
if (out_len == 0) {
10311037
*out = ArrayBuffer::NewBackingStore(env->isolate(), 0);
1038+
} else if (out_len != (*out)->ByteLength()) {
1039+
std::unique_ptr<BackingStore> old_out = std::move(*out);
1040+
*out = ArrayBuffer::NewBackingStore(env->isolate(), out_len);
1041+
memcpy(static_cast<char*>((*out)->Data()),
1042+
static_cast<char*>(old_out->Data()),
1043+
out_len);
1044+
}
10321045

10331046
return true;
10341047
}

‎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 if (sig_len != sig->ByteLength()) {
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

+7-2
Original file line numberDiff line numberDiff line change
@@ -2029,9 +2029,14 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
20292029

20302030
statistics_.data_received += nread;
20312031

2032-
if (LIKELY(stream_buf_offset_ == 0)) {
2032+
if (LIKELY(stream_buf_offset_ == 0 &&
2033+
static_cast<size_t>(nread) != bs->ByteLength())) {
20332034
// Shrink to the actual amount of used data.
2034-
bs = BackingStore::Reallocate(env()->isolate(), std::move(bs), nread);
2035+
std::unique_ptr<BackingStore> old_bs = std::move(bs);
2036+
bs = ArrayBuffer::NewBackingStore(env()->isolate(), nread);
2037+
memcpy(static_cast<char*>(bs->Data()),
2038+
static_cast<char*>(old_bs->Data()),
2039+
nread);
20352040
} else {
20362041
// This is a very unlikely case, and should only happen if the ReadStart()
20372042
// call in OnStreamAfterWrite() immediately provides data. If that does

‎src/stream_base.cc

+7-1
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,13 @@ 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);
682+
if (static_cast<size_t>(nread) != bs->ByteLength()) {
683+
std::unique_ptr<BackingStore> old_bs = std::move(bs);
684+
bs = ArrayBuffer::NewBackingStore(isolate, nread);
685+
memcpy(static_cast<char*>(bs->Data()),
686+
static_cast<char*>(old_bs->Data()),
687+
nread);
688+
}
683689

684690
stream->CallJSOnreadMethod(nread, ArrayBuffer::New(isolate, std::move(bs)));
685691
}

‎src/udp_wrap.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,13 @@ void UDPWrap::OnRecv(ssize_t nread,
764764
return;
765765
} else if (nread == 0) {
766766
bs = ArrayBuffer::NewBackingStore(isolate, 0);
767-
} else {
767+
} else if (static_cast<size_t>(nread) != bs->ByteLength()) {
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)
Please sign in to comment.