fix: DisconnectReason::decode should return error on zero length list (#7284)

This commit is contained in:
jn
2024-03-22 02:52:59 +07:00
committed by GitHub
parent 6eb7397aa2
commit ea5535cc28

View File

@ -129,9 +129,14 @@ impl Decodable for DisconnectReason {
// this should be a list, so decode the list header. this should advance the buffer so
// buf[0] is the first (and only) element of the list.
let header = Header::decode(buf)?;
if !header.list {
return Err(RlpError::UnexpectedString)
}
if header.payload_length != 1 {
return Err(RlpError::ListLengthMismatch { expected: 1, got: header.payload_length })
}
}
// geth rlp encodes [`DisconnectReason::DisconnectRequested`] as 0x00 and not as empty
@ -232,6 +237,14 @@ mod tests {
assert!(DisconnectReason::decode(&mut &[0u8; 3][..]).is_err())
}
#[test]
fn test_reason_zero_length_list() {
let list_with_zero_length = hex::decode("c000").unwrap();
let res = DisconnectReason::decode(&mut &list_with_zero_length[..]);
assert!(res.is_err());
assert_eq!(res.unwrap_err().to_string(), "unexpected list length (got 0, expected 1)")
}
#[test]
fn disconnect_encoding_length() {
let all_reasons = all_reasons();