Skip to content

Commit df69040

Browse files
committed
Implement raw::Decoder::flush using an empty run step
1 parent b8c1224 commit df69040

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/stream/raw.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,23 @@ impl Operation for Decoder<'_> {
184184
.map_err(map_error_code)
185185
}
186186

187+
fn flush<C: WriteBuf + ?Sized>(
188+
&mut self,
189+
output: &mut OutBuffer<'_, C>,
190+
) -> io::Result<usize> {
191+
// To flush, we just offer no additional input.
192+
self.run(&mut InBuffer::around(&[]), output)?;
193+
194+
// We don't _know_ how much (decompressed data) there is still in buffer.
195+
if output.pos() < output.dst.capacity() {
196+
// We only know when there's none (the output buffer is not full).
197+
Ok(0)
198+
} else {
199+
// Otherwise, pretend there's still "1 byte" remaining.
200+
Ok(1)
201+
}
202+
}
203+
187204
fn reinit(&mut self) -> io::Result<()> {
188205
self.context
189206
.reset(zstd_safe::ResetDirective::SessionOnly)

tests/issue_182.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const TEXT: &[u8] = include_bytes!("../assets/example.txt");
2+
3+
#[test]
4+
#[should_panic]
5+
fn test_issue_182() {
6+
use std::io::BufRead;
7+
8+
let compressed = zstd::encode_all(TEXT, 3).unwrap();
9+
let truncated = &compressed[..compressed.len() / 2];
10+
11+
let rdr = zstd::Decoder::new(truncated).unwrap();
12+
let rdr = std::io::BufReader::new(rdr);
13+
for line in rdr.lines() {
14+
line.unwrap();
15+
}
16+
}

0 commit comments

Comments
 (0)