Skip to content

Commit

Permalink
refactor!: Refactor WireMsg::read_from_stream to avoid logic error
Browse files Browse the repository at this point in the history
Previously we used `TryFrom` which we know will succeed thanks to our
assertions, however we don't want to allow for the possibility of
unexpected panics by using `unwrap` here.

Since we have asserted the pointer size though, we can reasonably
perform a lossy conversion with `as`, knowing that in fact nothing will
be lost.

BREAKING CHANGE: The `Error::UnexpectedError` variant has been removed.
  • Loading branch information
Chris Connelly authored and connec committed Aug 27, 2021
1 parent 3b8174a commit 047d071
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ pub enum Error {
/// Missing connection
#[error("No connection to the dest peer")]
MissingConnection,
/// A supposedly impossible internal error occurred
#[error("Unexpected internal error: {0}")]
UnexpectedError(String),
/// Couldn't resolve Public IP address
#[error("Unresolved Public IP address")]
UnresolvedPublicIp,
Expand Down
13 changes: 3 additions & 10 deletions src/wire_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,9 @@ impl WireMsg {
{
compile_error!("You need an architecture capable of addressing 32-bit pointers");
}
let data_length = match usize::try_from(msg_header.data_len()) {
Ok(s) => s,
Err(_) => {
return Err(Error::UnexpectedError(format!(
// this should never happen due to preceding compilation error
"unable to convert u32 '{}' to usize",
msg_header.data_len()
)));
}
};
// we know we can convert without loss thanks to our assertions above
let data_length = msg_header.data_len() as usize;

let mut data: Vec<u8> = vec![0; data_length];
let msg_flag = msg_header.usr_msg_flag();

Expand Down

0 comments on commit 047d071

Please sign in to comment.