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
15 changes: 9 additions & 6 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1170,12 +1170,19 @@ zstream_run_try(VALUE value_arg)
z->stream.avail_out = 0;
}

if (NIL_P(z->input) && len == 0) {
zstream_append_input(z, src, len);

/* expanding the buffer may yield the pending output, and the block
may exit there, so keep the input in `z->input' until then. */
if (z->stream.avail_out == 0) {
zstream_expand_buffer(z);
}

if (NIL_P(z->input)) {
z->stream.next_in = (Bytef*)"";
z->stream.avail_in = 0;
}
else {
zstream_append_input(z, src, len);
/* keep reference to `z->input' so as not to be garbage collected
after zstream_reset_input() and prevent `z->stream.next_in'
from dangling. */
Expand All @@ -1186,10 +1193,6 @@ zstream_run_try(VALUE value_arg)
args->rest_in = RSTRING_LEN(old_input) - z->stream.avail_in;
}

if (z->stream.avail_out == 0) {
zstream_expand_buffer(z);
}

loop:
err = zstream_run_func(args);

Expand Down
25 changes: 25 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,31 @@ def test_inflate_chunked_break
assert_equal 100_000 - chunks.first.length, out.length
end

def test_inflate_chunked_break_with_pending_output
# The first call leaves 16 KiB pending, which the second call yields
# before inflating its input.
src = "a" * 16_384 + "b" * 16_384
deflated = Zlib::Deflate.deflate(src, Zlib::NO_COMPRESSION)
# zlib header, stored block header, and the first 16 KiB of data
head = deflated.byteslice(0, 2 + 5 + 16_384)
rest = deflated.byteslice(head.bytesize..)

[false, true].product([:break, :raise]) do |use_buffer, exit_by|
opts = use_buffer ? {buffer: String.new} : {}
z = Zlib::Inflate.new
inflated = "".dup
z.inflate(head, **opts) { |chunk| inflated << chunk }
if exit_by == :break
z.inflate(rest, **opts) { |chunk| inflated << chunk; break }
else
assert_raise(RuntimeError) { z.inflate(rest, **opts) { |chunk| inflated << chunk; raise } }
end
assert_equal 16_384, inflated.bytesize
z.inflate(nil, **opts) { |chunk| inflated << chunk }
assert_equal src, inflated, "buffer: #{use_buffer}, #{exit_by}"
end
end

def test_inflate_dictionary
dictionary = "foo"

Expand Down
Loading