From 1b3c88a70c3e8044a7cb6e9d4a04810fc692fd30 Mon Sep 17 00:00:00 2001 From: Zoey Riordan Date: Tue, 23 Jun 2020 23:41:55 -0700 Subject: [PATCH] fix logic error in fill_buffer (#336) --- Cargo.toml | 2 +- src/de/read.rs | 27 +++++++++++++++++++++------ src/lib.rs | 2 +- tests/test.rs | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 029d09e21..89b895057 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 ", "Francesco Mazzoli ", "David Tolnay ", "Zoey Riordan "] exclude = ["logo.png", "examples/*", ".gitignore", ".travis.yml"] diff --git a/src/de/read.rs b/src/de/read.rs index 4179ad80a..80123f61c 100644 --- a/src/de/read.rs +++ b/src/de/read.rs @@ -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)?; @@ -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()); + } +} diff --git a/src/lib.rs b/src/lib.rs index e08f11b20..78b4d0446 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"] diff --git a/tests/test.rs b/tests/test.rs index fd6c829fe..82fba86c6 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -880,3 +880,21 @@ fn test_varint_length_prefixes() { (1 + std::mem::size_of::()) as u64 ); // 2 ** 16 + 1 } + +#[test] +fn test_byte_vec_struct() { + #[derive(PartialEq, Eq, Clone, Serialize, Deserialize, Debug)] + struct ByteVecs { + a: Vec, + b: Vec, + c: Vec, + }; + + let byte_struct = ByteVecs { + a: vec![2; 20], + b: vec![3; 30], + c: vec![1; 10], + }; + + the_same(byte_struct); +}