Skip to content

Commit

Permalink
fix logic error in fill_buffer (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoeyR committed Jun 24, 2020
1 parent f5fa06e commit 1b3c88a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bincode"
version = "1.3.0" # remember to update html_root_url
version = "1.3.1" # remember to update html_root_url
authors = ["Ty Overby <ty@pre-alpha.com>", "Francesco Mazzoli <f@mazzo.li>", "David Tolnay <dtolnay@gmail.com>", "Zoey Riordan <zoey@dos.cafe>"]
exclude = ["logo.png", "examples/*", ".gitignore", ".travis.yml"]

Expand Down
27 changes: 21 additions & 6 deletions src/de/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ where
R: io::Read,
{
fn fill_buffer(&mut self, length: usize) -> Result<()> {
// Reserve and fill extra space if needed
let current_length = self.temp_buffer.len();
if length > current_length {
self.temp_buffer.reserve_exact(length - current_length);
self.temp_buffer.resize(length, 0);
}
self.temp_buffer.resize(length, 0);

self.reader.read_exact(&mut self.temp_buffer)?;

Expand Down Expand Up @@ -185,3 +180,23 @@ where
visitor.visit_bytes(&self.temp_buffer[..])
}
}

#[cfg(test)]
mod test {
use super::IoReader;

#[test]
fn test_fill_buffer() {
let buffer = vec![0u8; 64];
let mut reader = IoReader::new(buffer.as_slice());

reader.fill_buffer(20).unwrap();
assert_eq!(20, reader.temp_buffer.len());

reader.fill_buffer(30).unwrap();
assert_eq!(30, reader.temp_buffer.len());

reader.fill_buffer(5).unwrap();
assert_eq!(5, reader.temp_buffer.len());
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//! Support for `i128` and `u128` is automatically enabled on Rust toolchains
//! greater than or equal to `1.26.0` and disabled for targets which do not support it

#![doc(html_root_url = "https://docs.rs/bincode/1.3.0")]
#![doc(html_root_url = "https://docs.rs/bincode/1.3.1")]
#![crate_name = "bincode"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
Expand Down
18 changes: 18 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,21 @@ fn test_varint_length_prefixes() {
(1 + std::mem::size_of::<u32>()) as u64
); // 2 ** 16 + 1
}

#[test]
fn test_byte_vec_struct() {
#[derive(PartialEq, Eq, Clone, Serialize, Deserialize, Debug)]
struct ByteVecs {
a: Vec<u8>,
b: Vec<u8>,
c: Vec<u8>,
};

let byte_struct = ByteVecs {
a: vec![2; 20],
b: vec![3; 30],
c: vec![1; 10],
};

the_same(byte_struct);
}

0 comments on commit 1b3c88a

Please sign in to comment.