Skip to content

Commit

Permalink
No unsafe in recv_inner
Browse files Browse the repository at this point in the history
No need to play with fire (uninitialized memory). Simply initialize the recv
buffer once at startup.
  • Loading branch information
mxinden committed Sep 27, 2024
1 parent 1bd20da commit 8137e73
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 deletions.
2 changes: 1 addition & 1 deletion neqo-bin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl<'a, H: Handler> Runner<'a, H> {
handler,
args,
timeout: None,
recv_buf: Vec::with_capacity(neqo_udp::RECV_BUF_SIZE),
recv_buf: vec![0; neqo_udp::RECV_BUF_SIZE],
send_buf: Vec::new(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion neqo-bin/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl ServerRunner {
server,
timeout: None,
sockets,
recv_buf: Vec::with_capacity(neqo_udp::RECV_BUF_SIZE),
recv_buf: vec![0; neqo_udp::RECV_BUF_SIZE],
send_buf: vec![],
}
}
Expand Down
22 changes: 5 additions & 17 deletions neqo-udp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,15 @@ use std::os::fd::AsFd as SocketRef;
#[cfg(windows)]
use std::os::windows::io::AsSocket as SocketRef;

/// # Panics
///
/// TODO
pub fn recv_inner<'a>(
local_address: &SocketAddr,
state: &UdpSocketState,
socket: impl SocketRef,
recv_buf: &'a mut Vec<u8>,
) -> Result<Datagram<&'a [u8]>, io::Error> {
// TODO: Worth it?
assert_eq!(recv_buf.capacity(), RECV_BUF_SIZE);
// TODO: unsafe worth it here?
unsafe {
recv_buf.set_len(RECV_BUF_SIZE);
}

let mut meta;

loop {
let data = loop {
meta = RecvMeta::default();

state.recv(
Expand All @@ -90,14 +80,12 @@ pub fn recv_inner<'a>(
continue;
}

recv_buf.truncate(meta.len);

break;
}
break &recv_buf[..meta.len];
};

qtrace!(
"received {} bytes from {} to {} with {} segments",
recv_buf.len(),
data.len(),
meta.addr,
local_address,
meta.len.div_ceil(meta.stride),
Expand All @@ -107,7 +95,7 @@ pub fn recv_inner<'a>(
meta.addr,
*local_address,
meta.ecn.map(|n| IpTos::from(n as u8)).unwrap_or_default(),
recv_buf,
data,
Some(meta.stride),
))
}
Expand Down

0 comments on commit 8137e73

Please sign in to comment.