Skip to content

Commit

Permalink
Removed unnecessary unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
manforowicz committed Mar 26, 2024
1 parent 6bf6303 commit bc3462a
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 533 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ members = [
"gday_server",
"gday_hole_punch",
"gday_encryption",
"gday_encryption_async",
"gday_contact_exchange_protocol",
"gday_file_offer_protocol",
]
9 changes: 9 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- Switch to a simple self-describing binary protocol such as MessagePack or CBOR.
- Figure out a simple way to use these formats with async.
- Have the hole puncher time out and return a helpful error after a given amount of time.
- Have the hole puncher prefer local sockets over public sockets.

- Allow the server to run without TLS.
- Allow the client to not use TLS.
- Improve error message everywhere in general. Have helpful tips to the user.
- Add checks in the file transfer to avoid TOCTOU bugs.
2 changes: 1 addition & 1 deletion gday/src/base32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn encode(mut num: u64) -> String {
s.push(b'0');
}

String::from_utf8(s).unwrap()
String::from_utf8(s).expect("Base 32 string is invalid. This shouldn't ever happen.")
}

#[derive(Error, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion gday/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn create_progress_bar(bytes: u64) -> ProgressBar {
let style = ProgressStyle::with_template(
"{msg} [{wide_bar}] {bytes}/{total_bytes} | {bytes_per_sec} | eta: {eta}",
)
.unwrap();
.expect("Progress bar style string was invalid.");
let draw = ProgressDrawTarget::stderr_with_hz(2);
ProgressBar::with_draw_target(Some(bytes), draw)
.with_style(style)
Expand Down
12 changes: 9 additions & 3 deletions gday_encryption/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ impl<T: Write> EncryptedStream<T> {
.encrypt_next_in_place(&[], &mut msg)
.map_err(|_| std::io::Error::new(ErrorKind::InvalidData, "Encryption error"))?;

let len = u16::try_from(msg.len()).unwrap().to_be_bytes();
let len = u16::try_from(msg.len())
.expect("unreachable: Length of message buffer should always fit in u16")
.to_be_bytes();

// write length to header
self.to_send[0..2].copy_from_slice(&len);
Expand All @@ -160,7 +162,9 @@ impl<T: Write> EncryptedStream<T> {
self.is_flushing = false;

// make space for new header
self.to_send.extend_from_slice(&[0, 0]).unwrap();
self.to_send
.extend_from_slice(&[0, 0])
.expect("unreachable: to_send must have space for the header.");
Ok(())
}
}
Expand Down Expand Up @@ -209,7 +213,9 @@ impl<T: Write> Write for EncryptedStream<T> {
}

let bytes_taken = std::cmp::min(buf.len(), self.to_send.spare_capacity().len() - TAG_SIZE);
self.to_send.extend_from_slice(&buf[..bytes_taken]).unwrap();
self.to_send.extend_from_slice(&buf[..bytes_taken]).expect(
"unreachable: bytes_taken is less than or equal to to_send.spare_capacity().len()",
);

if self.to_send.spare_capacity().len() == TAG_SIZE {
self.flush_write_buf()?;
Expand Down
18 changes: 0 additions & 18 deletions gday_encryption_async/Cargo.toml

This file was deleted.

Empty file removed gday_encryption_async/README.md
Empty file.
198 changes: 0 additions & 198 deletions gday_encryption_async/src/helper_buf.rs

This file was deleted.

13 changes: 0 additions & 13 deletions gday_encryption_async/src/lib.rs

This file was deleted.

Loading

0 comments on commit bc3462a

Please sign in to comment.