Skip to content
Open
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
7 changes: 7 additions & 0 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,13 @@ zstream_run_try(VALUE value_arg)
}
}

if (err == Z_BUF_ERROR && z->func == &deflate_funcs && z->stream.avail_in == 0) {
/* deflate() reports Z_BUF_ERROR for a flush when no input was consumed
* since the previous flush of the same or higher rank. Nothing is lost,
* so treat it as a no-op like zlib's own gzflush() does. */
err = Z_OK;
}

if (flush != Z_FINISH && err == Z_BUF_ERROR && z->stream.avail_out > 0) {
z->flags |= ZSTREAM_FLAG_IN_STREAM;
}
Expand Down
26 changes: 26 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ def test_flush
assert_equal("foobarbazqux", Zlib::Inflate.inflate(s))
end

def test_flush_twice
z = Zlib::Deflate.new
s = z.deflate("foo", Zlib::SYNC_FLUSH)
s << z.flush << z.flush
s << z.flush(Zlib::FULL_FLUSH) << z.flush(Zlib::SYNC_FLUSH)
s << z.deflate("", Zlib::SYNC_FLUSH)
s << z.finish
assert_equal("foo", Zlib::Inflate.inflate(s))
end

def test_avail
z = Zlib::Deflate.new
assert_equal(0, z.avail_in)
Expand Down Expand Up @@ -1535,6 +1545,22 @@ def test_puts
}
end

def test_flush_twice
io = StringIO.new("".b)
Zlib::GzipWriter.wrap(io) do |gz|
gz.write("a")
gz.flush
gz.flush
gz.flush(Zlib::FULL_FLUSH)
gz.flush(Zlib::SYNC_FLUSH)
gz.sync = true
gz.write("")
gz.write("")
gz.write("b")
end
assert_equal("ab", Zlib.gunzip(io.string))
end

def test_writer_wrap
Tempfile.create("test_zlib_gzip_writer_wrap") {|t|
t.binmode
Expand Down
Loading