added error ReadUintOutOfRange

This commit is contained in:
2025-01-04 16:01:04 +03:00
parent ead3797ea0
commit 3d7cf54723
+5 -2
View File
@@ -9,6 +9,8 @@ import "forge-std/console.sol";
* a token-based bytecode approach. * a token-based bytecode approach.
*/ */
contract Push3Interpreter { contract Push3Interpreter {
// ReadUint`numBits` out of the code range.
error ReadUintOutOfRange(uint256 numBits);
// ----------------------------------------------------- // -----------------------------------------------------
// 0. CONSTANTS // 0. CONSTANTS
// ----------------------------------------------------- // -----------------------------------------------------
@@ -137,14 +139,15 @@ contract Push3Interpreter {
* @dev Read `x` bytes from `code` at `start` as uint. * @dev Read `x` bytes from `code` at `start` as uint.
*/ */
function readUint(bytes calldata code, uint32 start, uint256 numBytes) internal pure returns (uint256 word) { function readUint(bytes calldata code, uint32 start, uint256 numBytes) internal pure returns (uint256 word) {
if (start + bytesNum) > code.length) revert ReadUintOutOfRange(); uint256 numBits = numBytes * 8;
if (start + bytesNum) > code.length) revert ReadUintOutOfRange(numBits);
assembly { assembly {
let buf := mload(0x40) // free memory let buf := mload(0x40) // free memory
// copy exactly x bytes from code into buf // copy exactly x bytes from code into buf
calldatacopy(buf, add(code.offset, start), numBytes) calldatacopy(buf, add(code.offset, start), numBytes)
word := mload(buf) word := mload(buf)
} }
word >>= 256 - numBytes * 8; word >>= 256 - numBits;
} }
/** /**