Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit 38b9a29

Browse files
authored
Add assertions (#21)
Adds some assertions that indicate user error. Resolves: #18
1 parent b27ce44 commit 38b9a29

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/nic/netlink.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<const N: usize> Buf<N> {
155155

156156
#[inline]
157157
fn read<P: Pod>(&self, off: &mut usize) -> Result<P> {
158-
if *off + P::size() > self.len {
158+
if *off > N || *off + P::size() > self.len {
159159
return Err(Error::new(
160160
ErrorKind::UnexpectedEof,
161161
"received incomplete netlink packet",
@@ -171,12 +171,10 @@ impl<const N: usize> Buf<N> {
171171

172172
#[inline]
173173
fn write<P: Pod>(&mut self, off: &mut usize, item: P) -> Result<()> {
174-
if *off + P::size() > self.len {
175-
return Err(Error::new(
176-
ErrorKind::UnexpectedEof,
177-
"received incomplete netlink packet",
178-
));
179-
}
174+
assert!(
175+
*off < N && *off + P::size() <= self.len,
176+
"this indicates a bug in the netlink code, please file an issue"
177+
);
180178

181179
// SAFETY: we've validated we'll only write within bounds
182180
unsafe {

src/packet.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ impl Packet {
423423
/// ```
424424
#[inline]
425425
pub fn read<T: Pod>(&self, offset: usize) -> Result<T, PacketError> {
426+
assert!(
427+
offset < 4096,
428+
"'offset' is wildly out of range and indicates a bug"
429+
);
430+
426431
let start = self.head + offset;
427432
if start > self.tail {
428433
return Err(PacketError::InvalidOffset {
@@ -499,6 +504,11 @@ impl Packet {
499504
/// ```
500505
#[inline]
501506
pub fn write<T: Pod>(&mut self, offset: usize, item: T) -> Result<(), PacketError> {
507+
assert!(
508+
offset < 4096,
509+
"'offset' is wildly out of range and indicates a bug"
510+
);
511+
502512
let start = self.head + offset;
503513
if start > self.tail {
504514
return Err(PacketError::InvalidOffset {
@@ -560,6 +570,23 @@ impl Packet {
560570
offset: usize,
561571
array: &mut [u8; N],
562572
) -> Result<(), PacketError> {
573+
struct AssertReasonable<const N: usize>;
574+
575+
impl<const N: usize> AssertReasonable<N> {
576+
const OK: () = assert!(N < 4096, "the array size far too large");
577+
}
578+
579+
const fn assert_reasonable<const N: usize>() {
580+
let () = AssertReasonable::<N>::OK;
581+
}
582+
583+
assert_reasonable::<N>();
584+
585+
assert!(
586+
offset < 4096,
587+
"'offset' is wildly out of range and indicates a bug"
588+
);
589+
563590
let start = self.head + offset;
564591
if start + N > self.tail {
565592
return Err(PacketError::InsufficientData {
@@ -619,6 +646,12 @@ impl Packet {
619646
/// ```
620647
#[inline]
621648
pub fn insert(&mut self, offset: usize, slice: &[u8]) -> Result<(), PacketError> {
649+
assert!(
650+
offset < 4096,
651+
"'offset' is wildly out of range and indicates a bug"
652+
);
653+
assert!(slice.len() <= 4096, "the slice length is far too large");
654+
622655
if self.tail + slice.len() > self.capacity {
623656
return Err(PacketError::InvalidPacketLength {});
624657
} else if offset > self.tail {

0 commit comments

Comments
 (0)