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: cobs accumulator out-of-bounds index when data is 1 byte too long #90

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
20 changes: 19 additions & 1 deletion src/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<const N: usize> CobsAccumulator<N> {
let (take, release) = input.split_at(n + 1);

// Does it fit?
if (self.idx + n) <= N {
if (self.idx + take.len()) <= N {
// Aw yiss - add to array
self.extend_unchecked(take);

Expand Down Expand Up @@ -286,3 +286,21 @@ fn double_loop_test_ref() {

assert!(Demo { a: 256854231, b: 115, c : "different test" } == demo2);
}

#[test]
fn extend_unchecked_in_bounds_test() {
// Test bug present in revision abcb407:
// extend_unchecked may be passed slice with size 1 greater than accumulator buffer causing panic

#[derive(serde::Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Demo {
data: [u8; 10],
}

// Accumulator has 1 byte less space than encoded message
let mut acc: CobsAccumulator<11> = CobsAccumulator::new();
let mut data = crate::to_vec_cobs::<_, 128>(&Demo { data: [0xcc; 10] }).unwrap();
assert_eq!(data.len(), 12); // 1 byte for offset + 1 sentinel byte appended

assert!(matches!(acc.feed::<Demo>(&data[..]), FeedResult::OverFull(_)));
}