From 001e625584be5339522ff9b734a8d8725291ff96 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Sun, 25 Oct 2026 09:54:23 +0900 Subject: [PATCH] Keep the input when the inflate block exits on pending output In the block form, a call that starts with 16 KiB of output left by the previous call yields it before taking any input. When the block broke or raised there, the input of that call was dropped and the stream failed later with Zlib::BufError. src = "a" * 16384 + "b" * 16384 data = Zlib::Deflate.deflate(src, 0) head = data.byteslice(0, 2 + 5 + 16384) inf = Zlib::Inflate.new out = +"" inf.inflate(head) { |c| out << c } inf.inflate(data.byteslice(head.bytesize..)) { |c| out << c; break } inf.inflate(nil) { |c| out << c } # expected: out == src # actual: Zlib::BufError Co-Authored-By: Claude Opus 5.5 --- ext/zlib/zlib.c | 15 +++++++++------ test/zlib/test_zlib.rb | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) 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"