Skip to content

Commit 0d388b2

Browse files
authored
codec: Decode smaller payloads for identity to None (#362)
This PR adds a tiny check to ensure smaller payloads than declared to the identify codec return None. cc @paritytech/networking Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
1 parent f70b794 commit 0d388b2

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/codec/identity.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Decoder for Identity {
5050
type Error = Error;
5151

5252
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
53-
if src.is_empty() {
53+
if src.is_empty() || src.len() < self.payload_len {
5454
return Ok(None);
5555
}
5656

@@ -96,6 +96,15 @@ mod tests {
9696
assert_eq!(decoded, copy);
9797
}
9898

99+
#[test]
100+
fn decoding_smaller_payloads() {
101+
let mut codec = Identity::new(100);
102+
let bytes = vec![3u8; 64];
103+
let mut bytes = BytesMut::from(&bytes[..]);
104+
105+
let decoded = codec.decode(&mut bytes);
106+
}
107+
99108
#[test]
100109
fn empty_encode() {
101110
let mut codec = Identity::new(32);

0 commit comments

Comments
 (0)