Skip to content

Commit c7909cc

Browse files
committed
fix(rlp): fix usize overflow in decode_bytes and decode_list (#33)
1 parent 4d48442 commit c7909cc

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

src/rlp.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,17 @@ pub fn decode_bytes(input: &[u8]) -> HcaResult<(Vec<u8>, usize)> {
253253
len
254254
)));
255255
}
256-
if input.len() < 1 + len_of_len + len {
256+
let total = (1usize)
257+
.checked_add(len_of_len)
258+
.and_then(|n| n.checked_add(len))
259+
.ok_or_else(|| HcaError::RlpDecodeError("length overflow".to_string()))?;
260+
if input.len() < total {
257261
return Err(HcaError::RlpDecodeError(format!(
258262
"long string payload truncated: need {} bytes",
259263
len
260264
)));
261265
}
262-
return Ok((
263-
input[1 + len_of_len..1 + len_of_len + len].to_vec(),
264-
1 + len_of_len + len,
265-
));
266+
return Ok((input[1 + len_of_len..total].to_vec(), total));
266267
}
267268

268269
Err(HcaError::RlpDecodeError(format!(
@@ -334,16 +335,17 @@ pub fn decode_list(input: &[u8]) -> HcaResult<(Vec<u8>, usize)> {
334335
));
335336
}
336337
let len = decode_usize_be(&input[1..1 + len_of_len])?;
337-
if input.len() < 1 + len_of_len + len {
338+
let total = (1usize)
339+
.checked_add(len_of_len)
340+
.and_then(|n| n.checked_add(len))
341+
.ok_or_else(|| HcaError::RlpDecodeError("length overflow".to_string()))?;
342+
if input.len() < total {
338343
return Err(HcaError::RlpDecodeError(format!(
339344
"long list payload truncated: need {} bytes",
340345
len
341346
)));
342347
}
343-
Ok((
344-
input[1 + len_of_len..1 + len_of_len + len].to_vec(),
345-
1 + len_of_len + len,
346-
))
348+
Ok((input[1 + len_of_len..total].to_vec(), total))
347349
}
348350

349351
/// Decode a raw HCA typed transaction.

0 commit comments

Comments
 (0)