Skip to content

Commit ae3c55d

Browse files
authored
fix(chunker): Don't underflow with wrong size_hint (#378)
see #377
1 parent 667e9fb commit ae3c55d

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

crates/core/src/chunker.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub(super) mod constants {
1616
pub(super) const MIN_SIZE: usize = 512 * KB;
1717
/// The maximum size of a chunk.
1818
pub(super) const MAX_SIZE: usize = 8 * MB;
19-
/// Buffer size used for reading.
20-
pub(super) const BUF_SIZE: usize = 64 * KB;
19+
/// Buffer size used for reading - TODO: Find out optimal size for best performance!
20+
pub(super) const BUF_SIZE: usize = 4 * KB;
2121
/// Random polynomial maximum tries.
2222
pub(super) const RAND_POLY_MAX_TRIES: i32 = 1_000_000;
2323
}
@@ -68,8 +68,8 @@ impl<R: Read + Send> ChunkIter<R> {
6868
/// * `rabin` - The rolling hash.
6969
pub(crate) fn new(reader: R, size_hint: usize, rabin: Rabin64) -> Self {
7070
Self {
71-
buf: Vec::with_capacity(4 * constants::KB),
72-
pos: 0,
71+
buf: vec![0; constants::BUF_SIZE],
72+
pos: constants::BUF_SIZE,
7373
reader,
7474
predicate: default_predicate,
7575
rabin,
@@ -137,8 +137,6 @@ impl<R: Read + Send> Iterator for ChunkIter<R> {
137137
}
138138

139139
if self.buf.len() == self.pos {
140-
// TODO: use a possibly uninitialized buffer here
141-
self.buf.resize(constants::BUF_SIZE, 0);
142140
match self.reader.read(&mut self.buf[..]) {
143141
Ok(0) => {
144142
self.finished = true;
@@ -165,7 +163,7 @@ impl<R: Read + Send> Iterator for ChunkIter<R> {
165163
self.pos += 1;
166164
self.rabin.slide(byte);
167165
}
168-
self.size_hint -= vec.len();
166+
self.size_hint = self.size_hint.saturating_sub(vec.len()); // size_hint can be too small!
169167
Some(Ok(vec))
170168
}
171169
}

0 commit comments

Comments
 (0)