Skip to content

Commit

Permalink
fix: DisconnectReason::decode should return error on zero length list (
Browse files Browse the repository at this point in the history
  • Loading branch information
0xqd authored and Ruteri committed Apr 17, 2024
1 parent a41e616 commit c4c594c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/net/eth-wire/src/disconnect.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c4c594c

Please sign in to comment.