Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/workerd/api/crypto/rsa.c++
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ jsg::BufferSource Rsa::cipher(jsg::Lock& js,

JSG_REQUIRE(labelCopy != nullptr, DOMOperationError,
"Failed to allocate space for RSA-OAEP label copy", tryDescribeOpensslErrors());
auto ptr = l.asArrayPtr();
std::copy(ptr.begin(), ptr.end(), labelCopy);
kj::arrayPtr(labelCopy, l.size()).copyFrom(l.asArrayPtr());

// EVP_PKEY_CTX_set0_rsa_oaep_label below takes ownership of the buffer passed in (must have
// been OPENSSL_malloc-allocated).
Expand Down
39 changes: 18 additions & 21 deletions src/workerd/api/streams/queue.c++
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,12 @@ bool ByteQueue::ByobRequest::respond(jsg::Lock& js, size_t amount) {
// Allocate the entry into which we will be copying the provided data for the
// other consumers of the queue.
KJ_IF_SOME(store, jsg::BufferSource::tryAlloc(js, amount)) {

auto entry = kj::heap<Entry>(kj::mv(store));

auto start = sourcePtr.begin() + req.pullInto.filled;
auto start = sourcePtr.slice(req.pullInto.filled);

// Safely copy the data over into the entry.
std::copy(start, start + amount, entry->toArrayPtr().begin());
entry->toArrayPtr().first(amount).copyFrom(start.first(amount));

// Push the entry into the other consumers.
queue.push(js, kj::mv(entry), consumer);
Expand Down Expand Up @@ -482,11 +481,11 @@ bool ByteQueue::ByobRequest::respond(jsg::Lock& js, size_t amount) {
consumer.resolveRead(js, req);

if (unaligned > 0) {
auto start = sourcePtr.begin() + (amount - unaligned);
auto start = sourcePtr.slice(amount - unaligned);

KJ_IF_SOME(store, jsg::BufferSource::tryAlloc(js, unaligned)) {
auto excess = kj::heap<Entry>(kj::mv(store));
std::copy(start, start + unaligned, excess->toArrayPtr().begin());
excess->toArrayPtr().first(unaligned).copyFrom(start.first(unaligned));
consumer.push(js, kj::mv(excess));
} else {
js.throwException(js.error("Failed to allocate memory for the byob read response."_kj));
Expand Down Expand Up @@ -631,7 +630,7 @@ void ByteQueue::handlePush(
auto sourcePtr = entry.entry->toArrayPtr();
auto sourceSize = sourcePtr.size() - entry.offset;

auto destPtr = pending.pullInto.store.asArrayPtr().begin() + pending.pullInto.filled;
auto destPtr = pending.pullInto.store.asArrayPtr().slice(pending.pullInto.filled);
auto destAmount = pending.pullInto.store.size() - pending.pullInto.filled;

// sourceSize is the amount of data remaining in the current entry to copy.
Expand All @@ -642,8 +641,8 @@ void ByteQueue::handlePush(
// so let's verify.
KJ_REQUIRE(sourceSize > 0 && sourceSize < destAmount);

// Safely copy amountToCopy bytes from sourcePtr to destPtr
std::copy(sourcePtr.begin() + entry.offset, sourcePtr.end(), destPtr);
// Safely copy sourceSize bytes from sourcePtr to destPtr
destPtr.first(sourceSize).copyFrom(sourcePtr.slice(entry.offset));

// We have completely consumed the data in this entry and can safely free
// our reference to it now. Yay!
Expand Down Expand Up @@ -691,9 +690,8 @@ void ByteQueue::handlePush(
// the entryOffset and pending.pullInto.filled offsets to determine the range
// where we start copying.
auto entryPtr = newEntry->toArrayPtr();
auto destPtr = pending.pullInto.store.asArrayPtr().begin() + pending.pullInto.filled;
std::copy(
entryPtr.begin() + entryOffset, entryPtr.begin() + entryOffset + amountToCopy, destPtr);
auto destPtr = pending.pullInto.store.asArrayPtr().slice(pending.pullInto.filled);
destPtr.first(amountToCopy).copyFrom(entryPtr.slice(entryOffset).first(amountToCopy));

// Yay! this pending read has been fulfilled. There might be more tho. Let's adjust
// the amountAvailable and continue trying to consume data.
Expand Down Expand Up @@ -773,10 +771,10 @@ void ByteQueue::handleRead(jsg::Lock& js,

// Once we have the amount, we safely copy amountToCopy bytes from the
// entry into the destination request, accounting properly for the offsets.
auto sourcePtr = entry.entry->toArrayPtr().begin() + entry.offset;
auto destPtr = request.pullInto.store.asArrayPtr().begin() + request.pullInto.filled;
auto sourcePtr = entry.entry->toArrayPtr().slice(entry.offset);
auto destPtr = request.pullInto.store.asArrayPtr().slice(request.pullInto.filled);

std::copy(sourcePtr, sourcePtr + amountToCopy, destPtr);
destPtr.first(amountToCopy).copyFrom(sourcePtr.first(amountToCopy));

request.pullInto.filled += amountToCopy;

Expand Down Expand Up @@ -900,7 +898,7 @@ bool ByteQueue::handleMaybeClose(
auto sourcePtr = entry.entry->toArrayPtr();
auto sourceSize = sourcePtr.size() - entry.offset;

auto destPtr = pending.pullInto.store.asArrayPtr().begin() + pending.pullInto.filled;
auto destPtr = pending.pullInto.store.asArrayPtr().slice(pending.pullInto.filled);
auto destAmount = pending.pullInto.store.size() - pending.pullInto.filled;

// There should be space available to copy into and data to copy from, or
Expand All @@ -912,15 +910,14 @@ bool ByteQueue::handleMaybeClose(
// destAmount is the amount of space remaining to be filled in the pending read.
auto amountToCopy = kj::min(sourceSize, destAmount);

auto sourceStart = sourcePtr.begin() + entry.offset;
auto sourceEnd = sourceStart + amountToCopy;
auto sourceStart = sourcePtr.slice(entry.offset);

// It shouldn't be possible for sourceEnd to extend past the sourcePtr.end()
// but let's make sure just to be safe.
KJ_ASSERT(sourceEnd <= sourcePtr.end());
KJ_ASSERT(amountToCopy <= sourceStart.size());

// Safely copy amountToCopy bytes from the source into the destination.
std::copy(sourceStart, sourceEnd, destPtr);
destPtr.first(amountToCopy).copyFrom(sourceStart.first(amountToCopy));
pending.pullInto.filled += amountToCopy;

// We do not need to adjust down the atLeast here because, no matter what,
Expand All @@ -931,8 +928,8 @@ bool ByteQueue::handleMaybeClose(

KJ_ASSERT(entry.offset <= sourcePtr.size());

if (sourceEnd == sourcePtr.end()) {
// If sourceEnd is equal to sourcePtr.end(), we've consumed the entire entry
if (amountToCopy == sourcePtr.size()) {
// If amountToCopy is equal to sourcePtr.size(), we've consumed the entire entry
// and we can free it.
auto released = kj::mv(next);
state.buffer.pop_front();
Expand Down
10 changes: 3 additions & 7 deletions src/workerd/api/streams/standard.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2769,14 +2769,10 @@ class AllReader {
}

void copyInto(kj::ArrayPtr<byte> out, PartList in) {
size_t pos = 0;
auto dest = out.begin();
for (auto& part: in) {
KJ_ASSERT(part.size() <= out.size() - pos);
auto ptr = part.begin();
std::copy(ptr, ptr + part.size(), dest);
pos += part.size();
dest += part.size();
KJ_ASSERT(part.size() <= out.size());
out.first(part.size()).copyFrom(part);
out = out.slice(part.size());
}
}
};
Expand Down