Skip to content
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
11 changes: 9 additions & 2 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,10 @@ zstream_sync(struct zstream *z, Bytef *src, rb_len_t len)
/* VALUE rest; */
int err;

if (z->flags & ZSTREAM_IN_PROGRESS) {
rb_raise(cInProgressError, "zlib stream is in progress");
}

if (!NIL_P(z->input)) {
z->stream.next_in = (Bytef*)RSTRING_PTR(z->input);
z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input));
Expand Down Expand Up @@ -2341,8 +2345,11 @@ rb_inflate_addstr(VALUE obj, VALUE src)
*
* Inputs +string+ into the end of input buffer and skips data until a full
* flush point can be found. If the point is found in the buffer, this method
* flushes the buffer and returns false. Otherwise it returns +true+ and the
* following data of full flush point is preserved in the buffer.
* returns +true+ and the following data of full flush point is preserved in
* the buffer. Otherwise it flushes the buffer and returns +false+.
*
* Raises a Zlib::InProgressError exception if called while the stream is
* being inflated.
*/
static VALUE
rb_inflate_sync(VALUE obj, VALUE src)
Expand Down
13 changes: 13 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,19 @@ def test_sync
assert_equal(true, z.sync("\0" * 100 + s))
end

def test_sync_in_progress
src = "x" * 100_000
z = Zlib::Inflate.new
inflated = "".dup
z.inflate(Zlib.deflate(src)) do |chunk|
if inflated.empty?
assert_raise(Zlib::InProgressError) { z.sync("\0\0\xff\xff".b) }
end
inflated << chunk
end
assert_equal src, inflated
end

def test_set_dictionary
z = Zlib::Inflate.new
assert_raise(Zlib::StreamError) { z.set_dictionary("foo") }
Expand Down
Loading