Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.
Open
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
19 changes: 13 additions & 6 deletions src/unsafe/Memory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,27 @@ library Memory {
// the bytes.
function copy(uint src, uint dest, uint len) internal pure {
// Copy word-length chunks while possible
// Reverse copy to prevent out of memory bound error
src = src + len;
dest = dest + len;
for (; len >= WORD_SIZE; len -= WORD_SIZE) {
dest -= WORD_SIZE;
src -= WORD_SIZE;

assembly {
mstore(dest, mload(src))
}
dest += WORD_SIZE;
src += WORD_SIZE;
}

if (len == 0) {
return;
}

// Copy remaining bytes
uint mask = 256 ** (WORD_SIZE - len) - 1;
src = src - len;
dest = dest - len;
assembly {
let srcpart := and(mload(src), not(mask))
let destpart := and(mload(dest), mask)
mstore(dest, or(destpart, srcpart))
mstore(dest, mload(src))
}
}

Expand Down