From 633a1c85ca3db6b9a5d44c7088f22449e56279e9 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Wed, 7 May 2025 21:54:42 +0200 Subject: [PATCH] Rewrite assembly slot offset for consistency (#5325) --- contracts/account/utils/draft-ERC7579Utils.sol | 2 +- contracts/governance/Governor.sol | 2 +- contracts/token/ERC1155/utils/ERC1155Utils.sol | 4 ++-- contracts/token/ERC20/utils/ERC1363Utils.sol | 4 ++-- contracts/token/ERC721/utils/ERC721Utils.sol | 2 +- contracts/utils/Address.sol | 3 +-- contracts/utils/Bytes.sol | 4 ++-- contracts/utils/Strings.sol | 4 ++-- 8 files changed, 12 insertions(+), 13 deletions(-) diff --git a/contracts/account/utils/draft-ERC7579Utils.sol b/contracts/account/utils/draft-ERC7579Utils.sol index 027d00df4..9b41cba1c 100644 --- a/contracts/account/utils/draft-ERC7579Utils.sol +++ b/contracts/account/utils/draft-ERC7579Utils.sol @@ -204,7 +204,7 @@ library ERC7579Utils { revert ERC7579DecodingError(); assembly ("memory-safe") { - executionBatch.offset := add(add(executionCalldata.offset, arrayLengthOffset), 32) + executionBatch.offset := add(add(executionCalldata.offset, arrayLengthOffset), 0x20) executionBatch.length := arrayLength } } diff --git a/contracts/governance/Governor.sol b/contracts/governance/Governor.sol index 9beb494df..09d15fc77 100644 --- a/contracts/governance/Governor.sol +++ b/contracts/governance/Governor.sol @@ -794,7 +794,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72 function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) { // This is not memory safe in the general case, but all calls to this private function are within bounds. assembly ("memory-safe") { - value := mload(add(buffer, add(0x20, offset))) + value := mload(add(add(buffer, 0x20), offset)) } } } diff --git a/contracts/token/ERC1155/utils/ERC1155Utils.sol b/contracts/token/ERC1155/utils/ERC1155Utils.sol index 346579393..76b273794 100644 --- a/contracts/token/ERC1155/utils/ERC1155Utils.sol +++ b/contracts/token/ERC1155/utils/ERC1155Utils.sol @@ -42,7 +42,7 @@ library ERC1155Utils { revert IERC1155Errors.ERC1155InvalidReceiver(to); } else { assembly ("memory-safe") { - revert(add(32, reason), mload(reason)) + revert(add(reason, 0x20), mload(reason)) } } } @@ -79,7 +79,7 @@ library ERC1155Utils { revert IERC1155Errors.ERC1155InvalidReceiver(to); } else { assembly ("memory-safe") { - revert(add(32, reason), mload(reason)) + revert(add(reason, 0x20), mload(reason)) } } } diff --git a/contracts/token/ERC20/utils/ERC1363Utils.sol b/contracts/token/ERC20/utils/ERC1363Utils.sol index 6ba26901e..e2ce20080 100644 --- a/contracts/token/ERC20/utils/ERC1363Utils.sol +++ b/contracts/token/ERC20/utils/ERC1363Utils.sol @@ -53,7 +53,7 @@ library ERC1363Utils { revert ERC1363InvalidReceiver(to); } else { assembly ("memory-safe") { - revert(add(32, reason), mload(reason)) + revert(add(reason, 0x20), mload(reason)) } } } @@ -87,7 +87,7 @@ library ERC1363Utils { revert ERC1363InvalidSpender(spender); } else { assembly ("memory-safe") { - revert(add(32, reason), mload(reason)) + revert(add(reason, 0x20), mload(reason)) } } } diff --git a/contracts/token/ERC721/utils/ERC721Utils.sol b/contracts/token/ERC721/utils/ERC721Utils.sol index 1ae7a600b..86f3f265b 100644 --- a/contracts/token/ERC721/utils/ERC721Utils.sol +++ b/contracts/token/ERC721/utils/ERC721Utils.sol @@ -41,7 +41,7 @@ library ERC721Utils { revert IERC721Errors.ERC721InvalidReceiver(to); } else { assembly ("memory-safe") { - revert(add(32, reason), mload(reason)) + revert(add(reason, 0x20), mload(reason)) } } } diff --git a/contracts/utils/Address.sol b/contracts/utils/Address.sol index 696f94ffa..d658e7af9 100644 --- a/contracts/utils/Address.sol +++ b/contracts/utils/Address.sol @@ -140,8 +140,7 @@ library Address { if (returndata.length > 0) { // The easiest way to bubble the revert reason is using memory via assembly assembly ("memory-safe") { - let returndata_size := mload(returndata) - revert(add(32, returndata), returndata_size) + revert(add(returndata, 0x20), mload(returndata)) } } else { revert Errors.FailedCall(); diff --git a/contracts/utils/Bytes.sol b/contracts/utils/Bytes.sol index f0708507f..1234b8455 100644 --- a/contracts/utils/Bytes.sol +++ b/contracts/utils/Bytes.sol @@ -93,7 +93,7 @@ library Bytes { // allocate and copy bytes memory result = new bytes(end - start); assembly ("memory-safe") { - mcopy(add(result, 0x20), add(buffer, add(start, 0x20)), sub(end, start)) + mcopy(add(result, 0x20), add(add(buffer, 0x20), start), sub(end, start)) } return result; @@ -108,7 +108,7 @@ library Bytes { function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) { // This is not memory safe in the general case, but all calls to this private function are within bounds. assembly ("memory-safe") { - value := mload(add(buffer, add(0x20, offset))) + value := mload(add(add(buffer, 0x20), offset)) } } } diff --git a/contracts/utils/Strings.sol b/contracts/utils/Strings.sol index e5aa2c550..4cc597646 100644 --- a/contracts/utils/Strings.sol +++ b/contracts/utils/Strings.sol @@ -48,7 +48,7 @@ library Strings { string memory buffer = new string(length); uint256 ptr; assembly ("memory-safe") { - ptr := add(buffer, add(32, length)) + ptr := add(add(buffer, 0x20), length) } while (true) { ptr--; @@ -484,7 +484,7 @@ library Strings { function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) { // This is not memory safe in the general case, but all calls to this private function are within bounds. assembly ("memory-safe") { - value := mload(add(buffer, add(0x20, offset))) + value := mload(add(add(buffer, 0x20), offset)) } } }