Binary protocol validation
Quick answer
A binary protocol parser turns untrusted bytes into checked values. Validate length, version and field values before using them.
Define a strict byte contract
Our toy packet has version 1, an unsigned one-byte payload length and exactly that many payload bytes. Limit the payload to 16 bytes. Check the header before indexing, reject unsupported versions and trailing bytes, and borrow the payload rather than copying. This parser assumes an entire packet is already available; streaming input needs a separate framing layer.
fn parse(b: &[u8]) -> Option<&[u8]> { if b.len() < 2 || b[0] != 1 { return None; } let n = b[1] as usize; if n > 16 || b.len() != n + 2 { return None; } Some(&b[2..])}fn main() { println!("{:?}", parse(&[1, 2, 7, 8]).unwrap()); println!("{}", parse(&[1, 3, 7]).is_none());}Expected stdout (not executed):
[7, 8]trueThe returned slice cannot outlive the input.
fn field(b: &[u8], start: usize, len: usize) -> Option<&[u8]> { b.get(start..start.checked_add(len)?)}fn main() { println!("{}", field(&[1,2], usize::MAX, 2).is_none());}Expected stdout (not executed):
trueChecked arithmetic prevents a wrapped end offset.
Common mistake
Never allocate or index solely on a length supplied by a peer.
Try it yourself
Add tests for empty input, wrong version and trailing bytes.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Binary protocol validation — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
The toy packet declares one payload byte but contains two. What does its exact-length contract require?
Binary protocol validation — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn parse(b: &[u8]) -> Option<&[u8]> { if b.len() < 2 || b[0] != 1 { return None; } let n = b[1] as usize; if n > 16 || b.len() != n + 2 { return None; } Some(&b[2..]) } fn main() { println!("{:?}", parse(&[1, 2, 7, 8]).unwrap()); println!("{}", parse(&[1, 3, 7]).is_none()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Binary protocol validation — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn field(b: &[u8], start: usize, len: usize) -> Option<&[u8]> { b.get(start..start.checked_add(len)?) } fn main() { println!("{}", field(&[1,2], usize::MAX, 2).is_none()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.