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

Handle bad length in header #155

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

This document describes the changes to Minimq between releases.

# [Unreleased]

## Fixed
* Fixed an issue where a corrupted mqtt header length could result in a crash

# [0.8.0] - 2023-11-01

## Changed
Expand Down
20 changes: 19 additions & 1 deletion src/de/packet_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ impl<'a> PacketReader<'a> {
self.read_bytes + 1
};

Ok(&mut self.buffer[self.read_bytes..end])
if end <= self.buffer.len() {
Ok(&mut self.buffer[self.read_bytes..end])
} else {
Err(Error::MalformedPacket)
}
}

pub fn commit(&mut self, count: usize) {
Expand Down Expand Up @@ -85,3 +89,17 @@ impl<'a> PacketReader<'a> {
ReceivedPacket::from_buffer(&self.buffer[..packet_length])
}
}

#[cfg(test)]
mod test {
use super::PacketReader;
#[test]
fn dont_panic_on_bad_data() {
let mut buffer: [u8; 4] = [0x20, 0x99, 0x00, 0x00];
let mut packet_reader = PacketReader::new(&mut buffer);
packet_reader.commit(4);
packet_reader
.receive_buffer()
.expect_err("parsed packet with invalid length");
}
}
Loading