From c507ab9fd610611ce6d06299856b6737c97f2ec5 Mon Sep 17 00:00:00 2001 From: Justin Berman Date: Thu, 11 Jan 2024 02:15:11 -0600 Subject: [PATCH] monero: match varint decoding (#513) * monero: match varint decoding * Fix build and clippy --- coins/monero/src/serialize.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/coins/monero/src/serialize.rs b/coins/monero/src/serialize.rs index fe7ca28f..916f306f 100644 --- a/coins/monero/src/serialize.rs +++ b/coins/monero/src/serialize.rs @@ -12,11 +12,21 @@ use curve25519_dalek::{ const VARINT_CONTINUATION_MASK: u8 = 0b1000_0000; mod sealed { - pub trait VarInt: TryInto + TryFrom + Copy {} - impl VarInt for u8 {} - impl VarInt for u32 {} - impl VarInt for u64 {} - impl VarInt for usize {} + pub trait VarInt: TryInto + TryFrom + Copy { + const BITS: usize; + } + impl VarInt for u8 { + const BITS: usize = 8; + } + impl VarInt for u32 { + const BITS: usize = 32; + } + impl VarInt for u64 { + const BITS: usize = 64; + } + impl VarInt for usize { + const BITS: usize = core::mem::size_of::() * 8; + } } // This will panic if the VarInt exceeds u64::MAX @@ -102,7 +112,7 @@ pub(crate) fn read_varint(r: &mut R) -> io::Result 64) && (b >= (1 << (64 - bits))) { + if ((bits + 7) >= U::BITS) && (b >= (1 << (U::BITS - bits))) { Err(io::Error::other("varint overflow"))?; }