Skip to content

Commit

Permalink
Remove unsound implementation of Bytes for slices and arrays
Browse files Browse the repository at this point in the history
Issue #308 tracks the actual fix.

Updates #308
  • Loading branch information
Thomasdezeeuw committed Oct 18, 2020
1 parent 8067105 commit 4f8731c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl Bytes for [MaybeUninit<u8>] {
}
}

/* TODO: this implementation is unsound, see issue #308.
impl Bytes for [u8] {
fn as_bytes(&mut self) -> &mut [MaybeUninit<u8>] {
// Safety: `MaybeUninit<u8>` is guaranteed to have the same layout as
Expand All @@ -111,6 +112,7 @@ impl Bytes for [u8] {
// Can't update the length of a slice.
}
}
*/

impl<const N: usize> Bytes for [MaybeUninit<u8>; N] {
fn as_bytes(&mut self) -> &mut [MaybeUninit<u8>] {
Expand All @@ -122,6 +124,7 @@ impl<const N: usize> Bytes for [MaybeUninit<u8>; N] {
}
}

/* TODO: this implementation is unsound, see issue #308.
impl<const N: usize> Bytes for [u8; N] {
fn as_bytes(&mut self) -> &mut [MaybeUninit<u8>] {
self.as_mut_slice().as_bytes()
Expand All @@ -131,6 +134,7 @@ impl<const N: usize> Bytes for [u8; N] {
// Can't update the length of an array.
}
}
*/

/// The implementation for `Vec<u8>` only uses the uninitialised capacity of the
/// vector. In other words the bytes currently in the vector remain untouched.
Expand Down
3 changes: 2 additions & 1 deletion src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ pub enum Connected {}
/// // echos the message to standard out.
/// async fn echo_server(mut ctx: actor::Context<Terminate>, local: SocketAddr) -> io::Result<()> {
/// let mut socket = UdpSocket::bind(&mut ctx, local)?;
/// let mut buf = [0; 4096];
/// let mut buf = Vec::with_capacity(4096);
/// loop {
/// buf.clear();
/// let mut receive_msg = ctx.receive_next().fuse();
/// let mut read = socket.recv_from(&mut buf).fuse();
/// let (n, address) = select! {
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ where
len
}

/* TODO: this implementation is unsound, see issue #308.
#[test]
fn impl_for_slice() {
let mut buf = vec![0; DATA.len() * 2].into_boxed_slice();
let n = write_bytes(DATA, buf.as_mut());
assert_eq!(n, DATA.len());
assert_eq!(&buf[..n], DATA);
}
*/

#[test]
fn impl_for_maybe_uninit_slice() {
Expand All @@ -44,13 +46,15 @@ fn impl_for_maybe_uninit_slice() {
);
}

/* TODO: this implementation is unsound, see issue #308.
#[test]
fn impl_for_array() {
let mut buf = [0; DATA.len() * 2];
let n = write_bytes(DATA, buf.as_mut());
assert_eq!(n, DATA.len());
assert_eq!(&buf[..n], DATA);
}
*/

#[test]
fn impl_for_maybe_uninit_array() {
Expand Down
6 changes: 4 additions & 2 deletions tests/functional/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ async fn unconnected_udp_actor(
let bytes_written = socket.send_to(&DATA, peer_address).await?;
assert_eq!(bytes_written, DATA.len());

let mut buf = [0; DATA.len() + 2];
let mut buf = Vec::with_capacity(DATA.len() + 2);
let (bytes_peeked, address) = socket.peek_from(&mut buf).await?;
assert_eq!(bytes_peeked, DATA.len());
assert_eq!(&buf[..bytes_peeked], &*DATA);
assert_eq!(address, peer_address);

buf.clear();
let (bytes_read, address) = socket.recv_from(&mut buf).await?;
assert_eq!(bytes_read, DATA.len());
assert_eq!(&buf[..bytes_read], &*DATA);
Expand All @@ -122,11 +123,12 @@ async fn connected_udp_actor(
let bytes_written = socket.send(&DATA).await?;
assert_eq!(bytes_written, DATA.len());

let mut buf = [0; DATA.len() + 2];
let mut buf = Vec::with_capacity(DATA.len() + 2);
let bytes_peeked = socket.peek(&mut buf).await?;
assert_eq!(bytes_peeked, DATA.len());
assert_eq!(&buf[..bytes_peeked], &*DATA);

buf.clear();
let bytes_read = socket.recv(&mut buf).await?;
assert_eq!(bytes_read, DATA.len());
assert_eq!(&buf[..bytes_read], &*DATA);
Expand Down

0 comments on commit 4f8731c

Please sign in to comment.