diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index e279bb9..e6cac24 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -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. */ @@ -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); diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index afe9529..c68effe 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -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"