Skip to content

Commit d90a597

Browse files
feat(common): enforce non-zero length for Datagram (#3070)
* feat(common): enforce non-zero length for Datagram Signed-off-by: WaterWhisperer <waterwhisperer24@qq.com> * Increase test coverage --------- Signed-off-by: WaterWhisperer <waterwhisperer24@qq.com> Co-authored-by: Max Leonard Inden <mail@max-inden.de>
1 parent a8f09fc commit d90a597

1 file changed

Lines changed: 35 additions & 13 deletions

File tree

neqo-common/src/datagram.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use std::{
1212

1313
use crate::{hex_with_len, Bytes, Tos};
1414

15+
/// A UDP datagram.
16+
///
17+
/// Guaranteed to not be empty.
1518
#[derive(Clone, PartialEq, Eq)]
1619
pub struct Datagram<D = Vec<u8>> {
1720
src: SocketAddr,
@@ -86,14 +89,14 @@ impl<D: AsMut<[u8]> + AsRef<[u8]>> AsMut<[u8]> for Datagram<D> {
8689
}
8790

8891
impl Datagram<Vec<u8>> {
92+
/// # Panics
93+
///
94+
/// Panics if `d` converts to an empty vector.
8995
#[must_use]
9096
pub fn new<V: Into<Vec<u8>>>(src: SocketAddr, dst: SocketAddr, tos: Tos, d: V) -> Self {
91-
Self {
92-
src,
93-
dst,
94-
tos,
95-
d: d.into(),
96-
}
97+
let d = d.into();
98+
assert!(!d.is_empty(), "Datagram data cannot be empty");
99+
Self { src, dst, tos, d }
97100
}
98101
}
99102

@@ -124,15 +127,23 @@ impl<D: AsRef<[u8]>> Debug for Datagram<D> {
124127
}
125128

126129
impl<'a> Datagram<&'a mut [u8]> {
130+
/// # Panics
131+
///
132+
/// Panics if the data is empty.
127133
#[must_use]
128134
pub fn from_slice(src: SocketAddr, dst: SocketAddr, tos: Tos, d: &'a mut [u8]) -> Self {
135+
assert!(!d.is_empty(), "Datagram data cannot be empty");
129136
Self { src, dst, tos, d }
130137
}
131138
}
132139

133140
impl Datagram<Bytes> {
141+
/// # Panics
142+
///
143+
/// Panics if the data is empty.
134144
#[must_use]
135-
pub const fn from_bytes(src: SocketAddr, dst: SocketAddr, tos: Tos, d: Bytes) -> Self {
145+
pub fn from_bytes(src: SocketAddr, dst: SocketAddr, tos: Tos, d: Bytes) -> Self {
146+
assert!(!d.is_empty(), "Datagram data cannot be empty");
136147
Self { src, dst, tos, d }
137148
}
138149
}
@@ -263,9 +274,9 @@ impl DatagramBatch {
263274
mod tests {
264275
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
265276

266-
use test_fixture::datagram;
277+
use test_fixture::{datagram, DEFAULT_ADDR};
267278

268-
use crate::{DatagramBatch, Ecn, Tos};
279+
use crate::{Datagram, DatagramBatch, Ecn, Tos};
269280

270281
#[test]
271282
fn fmt_datagram() {
@@ -277,10 +288,21 @@ mod tests {
277288
}
278289

279290
#[test]
280-
fn is_empty() {
281-
let d = datagram(vec![]);
282-
assert_eq!(d.len(), 0);
283-
assert!(d.is_empty());
291+
#[should_panic(expected = "Datagram data cannot be empty")]
292+
fn new_empty() {
293+
let _d = Datagram::new(DEFAULT_ADDR, DEFAULT_ADDR, Ecn::Ect0.into(), vec![]);
294+
}
295+
296+
#[test]
297+
#[should_panic(expected = "Datagram data cannot be empty")]
298+
fn from_slice_empty() {
299+
let _d = Datagram::from_slice(DEFAULT_ADDR, DEFAULT_ADDR, Ecn::Ect0.into(), &mut []);
300+
}
301+
302+
#[test]
303+
#[should_panic(expected = "Datagram data cannot be empty")]
304+
fn from_bytes_empty() {
305+
let _d = Datagram::from_bytes(DEFAULT_ADDR, DEFAULT_ADDR, Ecn::Ect0.into(), vec![].into());
284306
}
285307

286308
#[test]

0 commit comments

Comments
 (0)