Skip to content

Commit 7dc2e9e

Browse files
Merge pull request #117 from pierwill/fix-int-cast
fix: Use a `try_from` instead of a cast
2 parents 525f90c + 187dfbb commit 7dc2e9e

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/protocol/frame.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ where
4242
let len = Int32::read(&mut Cursor::new(len_buf))
4343
.expect("Reading Int32 from in-mem buffer should always work");
4444

45-
// convert to usize
46-
if len.0 < 0 {
47-
return Err(ReadError::NegativeMessageSize { size: len.0 });
48-
}
49-
let len = len.0 as usize;
45+
let len =
46+
usize::try_from(len.0).map_err(|_| ReadError::NegativeMessageSize { size: len.0 })?;
5047

5148
// check max message size to not blow up memory
5249
if len > max_message_size {
@@ -70,7 +67,7 @@ where
7067
});
7168
}
7269

73-
let mut buf = vec![0u8; len as usize];
70+
let mut buf = vec![0u8; len];
7471
self.read_exact(&mut buf).await?;
7572
Ok(buf)
7673
}

0 commit comments

Comments
 (0)