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 logic error in fill_buffer #336

Merged
merged 4 commits into from
Jun 24, 2020
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
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 <[email protected]>", "Francesco Mazzoli <[email protected]>", "David Tolnay <[email protected]>", "Zoey Riordan <[email protected]>"]
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);
}