Skip to content

Commit

Permalink
crypto: use ByteSource::Builder in To*Copy
Browse files Browse the repository at this point in the history
Avoid manual calls to MallocOpenSSL in ArrayBufferOrViewContents and
use the new ByteSource::Builder instead.

Refs: #43202

PR-URL: #43477
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
tniessen authored Jun 25, 2022
1 parent c7a0b14 commit 42ad967
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 42ad967

Please sign in to comment.