Skip to content

Commit 4764623

Browse files
authored
fix(sol-types): overflow in abi decoder (#982)
1 parent 49b7bce commit 4764623

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

crates/sol-types/src/abi/decoder.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ impl<'de> Decoder<'de> {
147147
/// advancing the offset.
148148
#[inline]
149149
pub fn peek_len_at(&self, offset: usize, len: usize) -> Result<&'de [u8], Error> {
150-
self.peek(offset..offset + len)
150+
let end = offset.checked_add(len).ok_or(Error::Overrun)?;
151+
self.peek(offset..end)
151152
}
152153

153154
/// Peek a slice of size `len` from the buffer without advancing the offset.
@@ -280,6 +281,7 @@ pub fn decode_sequence<'de, T: TokenSeq<'de>>(data: &'de [u8]) -> Result<T> {
280281

281282
#[cfg(test)]
282283
mod tests {
284+
use super::*;
283285
use crate::{SolType, SolValue, sol, sol_data, utils::pad_usize};
284286
use alloc::string::ToString;
285287
use alloy_primitives::{Address, B256, U256, address, bytes, hex};
@@ -720,4 +722,15 @@ mod tests {
720722

721723
assert_eq!(<Ty as SolType>::abi_decode(&encoded).unwrap(), ty);
722724
}
725+
726+
#[test]
727+
fn offset_overflow() {
728+
let encoded = hex!(
729+
"0000000000000000000000000000000000000000000000000000000000000020"
730+
"000000000000000000000000000000000000000000000000ffffffffffffffff"
731+
"0000000000000000000000000000000000000000000000000000000000000000"
732+
);
733+
let err = <sol_data::String as SolType>::abi_decode(&encoded).unwrap_err();
734+
assert_eq!(err, Error::Overrun);
735+
}
723736
}

crates/sol-types/src/impl_core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where
4848
Ok(unsafe { array_assume_init(array) })
4949
}
5050

51-
/// [`MaybeUninit::slice_assume_init_mut`]
51+
/// `MaybeUninit::slice_assume_init_mut`
5252
#[inline(always)]
5353
unsafe fn slice_assume_init_mut<T>(slice: &mut [MaybeUninit<T>]) -> &mut [T] {
5454
// SAFETY: similar to safety notes for `slice_get_ref`, but we have a

crates/sol-types/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
use crate::{Error, Result, Word};
1313

14-
const USIZE_BYTES: usize = usize::BITS as usize / 8;
14+
const USIZE_BYTES: usize = size_of::<usize>();
1515

1616
/// Calculates the padded length of a slice by rounding its length to the next
1717
/// word.

0 commit comments

Comments
 (0)