Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transport/packet): don't (mutably) borrow data multiple times #34

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions neqo-transport/src/packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,12 @@ pub struct PublicPacket<'a> {
/// The packet type.
packet_type: PacketType,
/// The recovered destination connection ID.
dcid: ConnectionIdRef<'a>,
dcid: ConnectionId,
/// The source connection ID, if this is a long header packet.
scid: Option<ConnectionIdRef<'a>>,
scid: Option<ConnectionId>,
/// Any token that is included in the packet (Retry always has a token; Initial sometimes
/// does). This is empty when there is no token.
token: &'a [u8],
token: Vec<u8>,
/// The size of the header, not including the packet number.
header_len: usize,
/// Protocol version, if present in header.
Expand Down Expand Up @@ -624,27 +624,28 @@ impl<'a> PublicPacket<'a> {
pub fn decode(
data: &'a mut [u8],
dcid_decoder: &dyn ConnectionIdDecoder,
) -> Res<(Self, &'a mut [u8])> {
) -> Res<(PublicPacket<'a>, &'a mut [u8])> {
let mut decoder = Decoder::new(data);
let first = Self::opt(decoder.decode_uint::<u8>())?;
let first = PublicPacket::opt(decoder.decode_uint::<u8>())?;

if first & 0x80 == PACKET_BIT_SHORT {
// Conveniently, this also guarantees that there is enough space
// for a connection ID of any size.
if decoder.remaining() < SAMPLE_OFFSET + SAMPLE_SIZE {
return Err(Error::InvalidPacket);
}
let dcid = Self::opt(dcid_decoder.decode_cid(&mut decoder))?;
let dcid = PublicPacket::opt(dcid_decoder.decode_cid(&mut decoder))?.into();
if decoder.remaining() < SAMPLE_OFFSET + SAMPLE_SIZE {
return Err(Error::InvalidPacket);
}
let header_len = decoder.offset();

return Ok((
Self {
PublicPacket {
packet_type: PacketType::Short,
dcid,
scid: None,
token: &[],
token: vec![],
header_len,
version: None,
data,
Expand All @@ -654,18 +655,18 @@ impl<'a> PublicPacket<'a> {
}

// Generic long header.
let version = Self::opt(decoder.decode_uint())?;
let dcid = ConnectionIdRef::from(Self::opt(decoder.decode_vec(1))?);
let scid = ConnectionIdRef::from(Self::opt(decoder.decode_vec(1))?);
let version = PublicPacket::opt(decoder.decode_uint())?;
let dcid = ConnectionIdRef::from(PublicPacket::opt(decoder.decode_vec(1))?).into();
let scid = ConnectionIdRef::from(PublicPacket::opt(decoder.decode_vec(1))?).into();

// Version negotiation.
if version == 0 {
return Ok((
Self {
PublicPacket {
packet_type: PacketType::VersionNegotiation,
dcid,
scid: Some(scid),
token: &[],
token: vec![],
header_len: decoder.offset(),
version: None,
data,
Expand All @@ -677,11 +678,11 @@ impl<'a> PublicPacket<'a> {
// Check that this is a long header from a supported version.
let Ok(version) = Version::try_from(version) else {
return Ok((
Self {
PublicPacket {
packet_type: PacketType::OtherVersion,
dcid,
scid: Some(scid),
token: &[],
token: vec![],
header_len: decoder.offset(),
version: Some(version),
data,
Expand All @@ -696,11 +697,12 @@ impl<'a> PublicPacket<'a> {
let packet_type = PacketType::from_byte((first >> 4) & 3, version);

// The type-specific code includes a token. This consumes the remainder of the packet.
let (token, header_len) = Self::decode_long(&mut decoder, packet_type, version)?;
let (token, header_len) = PublicPacket::decode_long(&mut decoder, packet_type, version)?;
let token = token.to_vec();
let end = data.len() - decoder.remaining();
let (data, remainder) = data.split_at_mut(end);
Ok((
Self {
PublicPacket {
packet_type,
dcid,
scid: Some(scid),
Expand Down Expand Up @@ -751,22 +753,24 @@ impl<'a> PublicPacket<'a> {
}

#[must_use]
pub const fn dcid(&self) -> ConnectionIdRef<'a> {
self.dcid
pub fn dcid(&self) -> ConnectionIdRef {
self.dcid.as_cid_ref()
}

/// # Panics
///
/// This will panic if called for a short header packet.
#[must_use]
pub fn scid(&self) -> ConnectionIdRef<'a> {
pub fn scid(&self) -> ConnectionIdRef {
self.scid
.as_ref()
.expect("should only be called for long header packets")
.as_cid_ref()
}

#[must_use]
pub const fn token(&self) -> &'a [u8] {
self.token
pub fn token(&self) -> &[u8] {
&self.token
}

#[must_use]
Expand Down