crypto: use ByteSource::Builder in To*Copy

Avoid manual calls to MallocOpenSSL in ArrayBufferOrViewContents and
use the new ByteSource::Builder instead.

Refs: https://github.com/nodejs/node/pull/43202

PR-URL: https://github.com/nodejs/node/pull/43477
Reviewed-By: Darshan Sen <raisinten@gmail.com>
This commit is contained in:
Tobias Nießen 2022-06-25 23:40:26 +02:00 committed by GitHub
parent c7a0b141b6
commit 42ad967d68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 9 deletions

View File

@ -747,19 +747,17 @@ class ArrayBufferOrViewContents {
inline ByteSource ToCopy() const {
if (size() == 0) return ByteSource();
char* buf = MallocOpenSSL<char>(size());
CHECK_NOT_NULL(buf);
memcpy(buf, data(), size());
return ByteSource::Allocated(buf, size());
ByteSource::Builder buf(size());
memcpy(buf.data<void>(), data(), size());
return std::move(buf).release();
}
inline ByteSource ToNullTerminatedCopy() const {
if (size() == 0) return ByteSource();
char* buf = MallocOpenSSL<char>(size() + 1);
CHECK_NOT_NULL(buf);
buf[size()] = 0;
memcpy(buf, data(), size());
return ByteSource::Allocated(buf, size());
ByteSource::Builder buf(size() + 1);
memcpy(buf.data<void>(), data(), size());
buf.data<char>()[size()] = 0;
return std::move(buf).release(size());
}
template <typename M>