Skip to content

Commit 81a6de9

Browse files
Seulgi Kimmergify[bot]
Seulgi Kim
authored andcommitted
Make Rlp handles corrupted data
1 parent c8123fd commit 81a6de9

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

util/rlp/src/rlpin.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ impl<'a> fmt::Display for Rlp<'a> {
136136
Ok(Prototype::Data(_)) => write!(f, "\"0x{}\"", self.data().unwrap().to_hex()),
137137
Ok(Prototype::List(len)) => {
138138
write!(f, "[")?;
139-
for i in 0..len - 1 {
140-
write!(f, "{}, ", self.at(i).unwrap())?;
139+
if len > 0 {
140+
for i in 0..len - 1 {
141+
write!(f, "{}, ", self.at(i).unwrap())?;
142+
}
143+
write!(f, "{}", self.at(len - 1).unwrap())?;
141144
}
142-
write!(f, "{}", self.at(len - 1).unwrap())?;
143145
write!(f, "]")
144146
}
145147
Err(err) => write!(f, "{:?}", err),
@@ -221,6 +223,19 @@ where
221223
// skip up to x items
222224
bytes = Rlp::consume_items(bytes, to_skip)?;
223225

226+
let payload_info = self.payload_info()?;
227+
let offset_max = payload_info.header_len + payload_info.value_len - 1;
228+
let new_offset = self.bytes.len() - bytes.len();
229+
// self.data.len() can be greater than byte length from payload's length
230+
// But you should not read the data which is lied after payload's length
231+
if new_offset > offset_max {
232+
return Err(DecoderError::RlpIsTooShort {
233+
expected: new_offset,
234+
got: offset_max,
235+
})
236+
}
237+
238+
224239
// update the cache
225240
self.offset_cache.set(OffsetCache::new(index, self.bytes.len() - bytes.len()));
226241

0 commit comments

Comments
 (0)