Skip to content

Commit baba152

Browse files
targosjoyeecheung
authored andcommitted
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 <[email protected]> PR-URL: #52292 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 1166e51 commit baba152

6 files changed

+58
-20
lines changed

src/crypto/crypto_cipher.cc

+23-10
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 if (static_cast<size_t>(buf_len) != (*out)->ByteLength()) {
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().
@@ -911,11 +916,14 @@ bool CipherBase::Final(std::unique_ptr<BackingStore>* out) {
911916
&out_len) == 1;
912917

913918
CHECK_LE(static_cast<size_t>(out_len), (*out)->ByteLength());
914-
if (out_len > 0) {
915-
*out =
916-
BackingStore::Reallocate(env()->isolate(), std::move(*out), out_len);
917-
} else {
919+
if (out_len == 0) {
918920
*out = ArrayBuffer::NewBackingStore(env()->isolate(), 0);
921+
} else if (static_cast<size_t>(out_len) != (*out)->ByteLength()) {
922+
std::unique_ptr<BackingStore> old_out = std::move(*out);
923+
*out = ArrayBuffer::NewBackingStore(env()->isolate(), out_len);
924+
memcpy(static_cast<char*>((*out)->Data()),
925+
static_cast<char*>(old_out->Data()),
926+
out_len);
919927
}
920928

921929
if (ok && kind_ == kCipher && IsAuthenticatedMode()) {
@@ -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) {
10211027
*out = ArrayBuffer::NewBackingStore(env->isolate(), 0);
1028+
} else if (out_len != (*out)->ByteLength()) {
1029+
std::unique_ptr<BackingStore> old_out = std::move(*out);
1030+
*out = ArrayBuffer::NewBackingStore(env->isolate(), out_len);
1031+
memcpy(static_cast<char*>((*out)->Data()),
1032+
static_cast<char*>(old_out->Data()),
1033+
out_len);
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 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)