Skip to content

Commit

Permalink
Merge pull request #30 from anatawa12/infinite-loop-with-broken
Browse files Browse the repository at this point in the history
Infinite loop in Deflate64Decoder with empty output buffer
  • Loading branch information
anatawa12 authored Jul 16, 2024
2 parents 48afa41 + 12380be commit ee2d75d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog].
### Removed

### Fixed
- Infinite loop with empty output buffer in `Deflate64Decoder` [`#30`](https://github.com/anatawa12/deflate64-rs/pull/30)

### Security

Expand Down
5 changes: 5 additions & 0 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ impl<R> Deflate64Decoder<R> {

impl<R: BufRead> Read for Deflate64Decoder<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if buf.is_empty() {
// we received empty buffer, so it won't be possible to write anything
return Ok(0);
}

loop {
let input = self.inner.fill_buf()?;
let eof = input.is_empty();
Expand Down
Binary file added test-assets/issue-29/raw.zip
Binary file not shown.
33 changes: 33 additions & 0 deletions tests/issue-29.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::io::{Cursor, Read};

use deflate64::Deflate64Decoder;

static ZIP_FILE_DATA: &[u8] = include_bytes!("../test-assets/issue-29/raw.zip");

fn deflate64_data() -> &'static [u8] {
&ZIP_FILE_DATA[121..]
}

// panic with invalid deflate64 data (too big lookup length)

#[test]
fn issue_29() {
let mut file = Deflate64Decoder::new(Cursor::new(deflate64_data()));
let mut buf = Vec::new();
let _ = file.read(&mut buf);
}

static VALID_ZIP_FILE_DATA: &[u8] = include_bytes!("../test-assets/deflate64.zip");
const BINARY_WAV_DATA_OFFSET: usize = 40;
const BINARY_WAV_COMPRESSED_SIZE: usize = 2669743;

fn valid_zip_source_stream() -> &'static [u8] {
&VALID_ZIP_FILE_DATA[BINARY_WAV_DATA_OFFSET..][..BINARY_WAV_COMPRESSED_SIZE]
}
#[test]
fn binary_wav() {
let mut decoder = Deflate64Decoder::new(Cursor::new(valid_zip_source_stream()));
let mut output = [];
let read = decoder.read(&mut output).unwrap();
assert_eq!(read, 0);
}

0 comments on commit ee2d75d

Please sign in to comment.