Remove possible panic in monero-serai on systems < 32 bits

This was done by requiring the system's usize can represent a certain number.
This commit is contained in:
Luke Parker
2024-07-04 02:27:14 -04:00
parent 3de89c717d
commit 0f9a5afa07

View File

@@ -169,12 +169,18 @@ impl Timelock {
/// Read a Timelock. /// Read a Timelock.
pub fn read<R: Read>(r: &mut R) -> io::Result<Self> { pub fn read<R: Read>(r: &mut R) -> io::Result<Self> {
const TIMELOCK_BLOCK_THRESHOLD: usize = 500_000_000;
let raw = read_varint::<_, u64>(r)?; let raw = read_varint::<_, u64>(r)?;
Ok(if raw == 0 { Ok(if raw == 0 {
Timelock::None Timelock::None
} else if raw < u64::from(500_000_000u32) { } else if raw <
// TODO: const-assert 32 or 64 bits u64::try_from(TIMELOCK_BLOCK_THRESHOLD)
Timelock::Block(usize::try_from(raw).expect("timelock (<32 bits) overflowed usize")) .expect("TIMELOCK_BLOCK_THRESHOLD didn't fit in a u64")
{
Timelock::Block(usize::try_from(raw).expect(
"timelock overflowed usize despite being less than a const representable with a usize",
))
} else { } else {
Timelock::Time(raw) Timelock::Time(raw)
}) })