Skip to content

Commit ef973fc

Browse files
committed
making unsafeReadBytesOffset private
1 parent 2abfa49 commit ef973fc

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

.changeset/rude-cougars-look.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

contracts/governance/Governor.sol

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {IERC165, ERC165} from "../utils/introspection/ERC165.sol";
1111
import {SafeCast} from "../utils/math/SafeCast.sol";
1212
import {DoubleEndedQueue} from "../utils/structs/DoubleEndedQueue.sol";
1313
import {Address} from "../utils/Address.sol";
14-
import {Bytes} from "../utils/Bytes.sol";
1514
import {Context} from "../utils/Context.sol";
1615
import {Nonces} from "../utils/Nonces.sol";
1716
import {Strings} from "../utils/Strings.sol";
@@ -770,7 +769,7 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
770769
}
771770

772771
// Extract what would be the `#proposer=` marker beginning the suffix
773-
bytes10 marker = bytes10(Bytes.unsafeReadBytesOffset(bytes(description), length - 52));
772+
bytes10 marker = bytes10(unsafeReadBytesOffset(bytes(description), length - 52));
774773

775774
// If the marker is not found, there is no proposer suffix to check
776775
if (marker != bytes10("#proposer=")) {
@@ -807,4 +806,17 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
807806
* @inheritdoc IGovernor
808807
*/
809808
function quorum(uint256 timepoint) public view virtual returns (uint256);
809+
810+
/**
811+
* @dev Reads a bytes32 from a bytes array without bounds checking.
812+
*
813+
* NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
814+
* assembly block as such would prevent some optimizations.
815+
*/
816+
function unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
817+
// This is not memory safe in the general case, but all calls to this private function are within bounds.
818+
assembly ("memory-safe") {
819+
value := mload(add(buffer, add(0x20, offset)))
820+
}
821+
}
810822
}

contracts/utils/Bytes.sol

Lines changed: 0 additions & 18 deletions
This file was deleted.

contracts/utils/Strings.sol

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ pragma solidity ^0.8.20;
66
import {Math} from "./math/Math.sol";
77
import {SafeCast} from "./math/SafeCast.sol";
88
import {SignedMath} from "./math/SignedMath.sol";
9-
import {Bytes} from "./Bytes.sol";
109

1110
/**
1211
* @dev String operations.
1312
*/
1413
library Strings {
15-
using Bytes for bytes;
1614
using SafeCast for *;
1715

1816
bytes16 private constant HEX_DIGITS = "0123456789abcdef";
@@ -237,8 +235,8 @@ library Strings {
237235
bytes memory buffer = bytes(input);
238236

239237
// Check presence of a negative sign.
240-
bytes1 sign = bytes1(buffer.unsafeReadBytesOffset(begin));
241-
bool positiveSign = sign == bytes1("+");
238+
bytes1 sign = bytes1(unsafeReadBytesOffset(buffer, begin));
239+
bool positiveSign = sign == bytes1("+");
242240
bool negativeSign = sign == bytes1("-");
243241
uint256 offset = (positiveSign || negativeSign).toUint();
244242

@@ -299,7 +297,7 @@ library Strings {
299297
bytes memory buffer = bytes(input);
300298

301299
// skip 0x prefix if present
302-
bool hasPrefix = bytes2(buffer.unsafeReadBytesOffset(begin)) == bytes2("0x");
300+
bool hasPrefix = bytes2(unsafeReadBytesOffset(buffer, begin)) == bytes2("0x");
303301
uint256 offset = hasPrefix.toUint() * 2;
304302

305303
uint256 result = 0;
@@ -357,7 +355,7 @@ library Strings {
357355
uint256 end
358356
) internal pure returns (bool success, address value) {
359357
// check that input is the correct length
360-
bool hasPrefix = bytes2(bytes(input).unsafeReadBytesOffset(begin)) == bytes2("0x");
358+
bool hasPrefix = bytes2(unsafeReadBytesOffset(bytes(input), begin)) == bytes2("0x");
361359
uint256 expectedLength = 40 + hasPrefix.toUint() * 2;
362360

363361
if (end - begin == expectedLength) {
@@ -386,4 +384,17 @@ library Strings {
386384

387385
return value;
388386
}
387+
388+
/**
389+
* @dev Reads a bytes32 from a bytes array without bounds checking.
390+
*
391+
* NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
392+
* assembly block as such would prevent some optimizations.
393+
*/
394+
function unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
395+
// This is not memory safe in the general case, but all calls to this private function are within bounds.
396+
assembly ("memory-safe") {
397+
value := mload(add(buffer, add(0x20, offset)))
398+
}
399+
}
389400
}

0 commit comments

Comments
 (0)