Skip to content

Commit

Permalink
Enable overflow check for extra safety
Browse files Browse the repository at this point in the history
  • Loading branch information
rory-ocl committed Aug 28, 2024
1 parent 7174c2a commit 8d1b45f
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions stylus-sdk/src/abi/ints.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023-2024, Offchain Labs, Inc.
// For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/stylus/licenses/COPYRIGHT.md

//! Support for generic integer types, found in [alloy_primitives].
//! Support for generic integer types found in [alloy_primitives].
use alloy_primitives::{ruint::UintTryFrom, Signed, Uint};
use alloy_sol_types::{
Expand Down Expand Up @@ -205,21 +205,35 @@ impl<const FB: usize, const FL: usize, const TB: usize, const TL: usize> From<Si
limbs[..slice.len()].copy_from_slice(slice);
Converted(Signed::from_limbs(limbs))
} else {
let (head, _tail) = slice.split_at(TL);
let (head, tail) = slice.split_at(TL);
let mut limbs = [0; TL];
limbs.copy_from_slice(head);
/* TODO overflow check needed?
let mut overflow = tail.iter().any(|&limb| limb != 0);
if TL > 0 {
overflow |= limbs[TL - 1] > Signed::<TB, TL>::MASK;
limbs[TL - 1] &= Signed::<TB, TL>::MASK;
overflow |= limbs[TL - 1] > mask(TB);
limbs[TL - 1] &= mask(TB);
}
if overflow {
// This should not happen.
panic!("overflow in int conversion");
}
*/
Converted(Signed::from_limbs(limbs))
}
}
}

const fn mask(bits: usize) -> u64 {
if bits == 0 {
return 0;
}
let bits = bits % 64;
if bits == 0 {
u64::MAX
} else {
(1 << bits) - 1
}
}

impl<T: Builtin, const BITS: usize, const LIMBS: usize> From<Uint<BITS, LIMBS>> for Converted<T>
where
IntBitCount<BITS>: SupportedInt<Uint = T>,
Expand Down

0 comments on commit 8d1b45f

Please sign in to comment.