Skip to content

Commit

Permalink
fix: incorrect read_slice impl for ReadAdapter
Browse files Browse the repository at this point in the history
This commit fixes a bug that is present in the implementation of
ByteReader for ReadAdapter. Specifically, it does not consume any input,
so calling `read_slice` and then attempting to read the next value in
the input, will read the same bytes again. The documented behavior of
this function is that any of the `read_*` methods consume the
corresponding amount of input.

To catch this, and to prevent future regressions, a new test was added
that serializes some data to a file using one approach, and deserializes
it using the ReadAdapter. This ensures that we don't accidentally make
choices in the writer that aren't matched by the reader, and vice versa.

Closes #308
  • Loading branch information
bitwalker committed Sep 6, 2024
1 parent 3345055 commit 6d86ac9
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion utils/core/src/serde/byte_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ impl<'a> ByteReader for ReadAdapter<'a> {
// this will return an error if we hit EOF first
self.buffer_at_least(len)?;

Ok(&self.buffer()[0..len])
let slice = &self.buf[self.pos..(self.pos + len)];
self.pos += len;
Ok(slice)
}

#[inline]
Expand Down Expand Up @@ -740,4 +742,35 @@ mod tests {

assert_eq!(adapter.read_usize(), Ok(VALUE));
}

#[test]
fn read_adapter_for_file() {
use crate::ByteWriter;
use std::fs::File;

let path = std::env::temp_dir().join("read_adapter_for_file.bin");

// Encode some data to a buffer, then write that buffer to a file
{
let mut buf = Vec::<u8>::with_capacity(256);
buf.write_bytes(b"MAGIC\0");
buf.write_bool(true);
buf.write_u32(0xbeef);
buf.write_usize(0xfeed);
buf.write_u16(0x5);

std::fs::write(&path, &buf).unwrap();
}

// Open the file, and try to decode the encoded items
let mut file = File::open(&path).unwrap();
let mut reader = ReadAdapter::new(&mut file);
assert_eq!(reader.peek_u8().unwrap(), b'M');
assert_eq!(reader.read_slice(6).unwrap(), b"MAGIC\0");
assert!(reader.read_bool().unwrap());
assert_eq!(reader.read_u32().unwrap(), 0xbeef);
assert_eq!(reader.read_usize().unwrap(), 0xfeed);
assert_eq!(reader.read_u16().unwrap(), 0x5);
assert!(!reader.has_more_bytes(), "expected there to be no more data in the input");
}
}

0 comments on commit 6d86ac9

Please sign in to comment.