Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace all usage of U256::from by const for usize::MAX in core #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions core/src/eval/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ macro_rules! op3_u256_fn {

macro_rules! as_usize_or_fail {
( $v:expr ) => {{
if $v > U256::from(usize::MAX) {
if $v > crate::utils::U256_USIZE_MAX {
return Control::Exit(ExitFatal::NotSupported.into());
}

$v.as_usize()
}};

( $v:expr, $reason:expr ) => {{
if $v > U256::from(usize::MAX) {
if $v > crate::utils::U256_USIZE_MAX {
return Control::Exit($reason.into());
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/eval/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn calldataload(state: &mut Machine) -> Control {
#[allow(clippy::needless_range_loop)]
for i in 0..32 {
if let Some(p) = index.checked_add(U256::from(i)) {
if p <= U256::from(usize::MAX) {
if p <= crate::utils::U256_USIZE_MAX {
let p = p.as_usize();
if p < state.data.len() {
load[i] = state.data[p];
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ impl Machine {

/// Copy and get the return value of the machine, if any.
pub fn return_value(&self) -> Vec<u8> {
if self.return_range.start > U256::from(usize::MAX) {
if self.return_range.start > crate::utils::U256_USIZE_MAX {
let mut ret = Vec::new();
ret.resize(
(self.return_range.end - self.return_range.start).as_usize(),
0,
);
ret
} else if self.return_range.end > U256::from(usize::MAX) {
} else if self.return_range.end > crate::utils::U256_USIZE_MAX {
let mut ret = self.memory.get(
self.return_range.start.as_usize(),
usize::MAX - self.return_range.start.as_usize(),
Expand Down
6 changes: 3 additions & 3 deletions core/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,20 @@ impl Memory {
len: U256,
data: &[u8],
) -> Result<(), ExitFatal> {
let memory_offset = if memory_offset > U256::from(usize::MAX) {
let memory_offset = if memory_offset > crate::utils::U256_USIZE_MAX {
return Err(ExitFatal::NotSupported);
} else {
memory_offset.as_usize()
};

let ulen = if len > U256::from(usize::MAX) {
let ulen = if len > crate::utils::U256_USIZE_MAX {
return Err(ExitFatal::NotSupported);
} else {
len.as_usize()
};

let data = if let Some(end) = data_offset.checked_add(len) {
if end > U256::from(usize::MAX) {
if end > crate::utils::U256_USIZE_MAX {
&[]
} else {
let data_offset = data_offset.as_usize();
Expand Down
7 changes: 7 additions & 0 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ use core::cmp::Ordering;
use core::ops::{Div, Rem};
use primitive_types::U256;

pub const U256_USIZE_MAX: U256 = {
// TODO: assert!(core::mem::size_of::<usize>() <= core::mem::size_of::<u64>());
let mut ret = [0; 4];
ret[0] = usize::MAX as u64;
U256(ret)
};

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Sign {
Plus,
Expand Down