diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 2b485746dff5df..624eb92b930c12 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -720,15 +720,19 @@ MaybeLocal StringBytes::Encode(Isolate* isolate, // Buffer, so we need to reorder on BE platforms. See // http://nodejs.org/api/buffer.html regarding Node's "ucs2" // encoding specification - std::vector dst; if (IsBigEndian()) { - dst.assign(buf, buf + buflen); - size_t nbytes = buflen * sizeof(dst[0]); - SwapBytes16(reinterpret_cast(&dst[0]), nbytes); - buf = &dst[0]; + uint16_t* dst = node::UncheckedMalloc(buflen); + if (dst == nullptr) { + *error = SB_MALLOC_FAILED_ERROR; + return MaybeLocal(); + } + size_t nbytes = buflen * sizeof(uint16_t); + memcpy(dst, buf, nbytes); + SwapBytes16(reinterpret_cast(dst), nbytes); + return ExternTwoByteString::New(isolate, dst, buflen, error); + } else { + return ExternTwoByteString::NewFromCopy(isolate, buf, buflen, error); } - - return ExternTwoByteString::NewFromCopy(isolate, buf, buflen, error); } MaybeLocal StringBytes::Encode(Isolate* isolate,