From f1c91378fc6195218a5b1bc149c832ff2076480f Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Mon, 9 Nov 2026 15:45:06 +0900 Subject: [PATCH 1/2] Put back the unconsumed input when the stream is interrupted An interrupt such as Thread#raise or Timeout raised in the middle of zstream_run left the unconsumed input in the detached string and next_in pointing to it, which Zlib::Deflate#params could read after it was freed. Put the input back and clear next_in before raising again. Co-Authored-By: Claude Opus 5.5 --- ext/zlib/zlib.c | 17 +++++++++++++++-- test/zlib/test_zlib.rb | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index e279bb9..f28af0a 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -1146,6 +1146,12 @@ zstream_run_func(struct zstream_run_args *args) return err; } +static VALUE +zstream_run_func_protect(VALUE args) +{ + return (VALUE)zstream_run_func((struct zstream_run_args *)args); +} + static VALUE zstream_run_try(VALUE value_arg) { @@ -1155,7 +1161,7 @@ zstream_run_try(VALUE value_arg) rb_len_t len = args->len; int flush = args->flush; - int err; + int err, state; VALUE old_input = Qnil; /* Cannot start zstream while it is in progress. */ @@ -1191,7 +1197,14 @@ zstream_run_try(VALUE value_arg) } loop: - err = zstream_run_func(args); + err = (int)rb_protect(zstream_run_func_protect, (VALUE)args, &state); + if (state) { + zstream_reset_input(z); + zstream_append_input(z, z->stream.next_in, z->stream.avail_in + args->rest_in); + z->stream.next_in = Z_NULL; + z->stream.avail_in = 0; + rb_jump_tag(state); + } /* retry if no exception is thrown */ if (err == Z_OK && args->interrupt) { diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index afe9529..7dc0682 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -227,6 +227,27 @@ def test_closed_p assert_equal(true, z.closed?) end + def test_params_after_interrupted_deflate + assert_separately(%w[-rzlib], "#{<<~"begin;"}\n#{<<~'end;'}", timeout: 60) + begin; + data = Random.new(0).bytes(8 << 20) + z = nil + [0.001, 0.005, 0.01, 0.02, 0.05, 0.1].cycle(3) do |wait| + z = Zlib::Deflate.new(1) + th = Thread.new { z.deflate(data) rescue nil } + sleep wait + th.raise(RuntimeError) + th.join + break if z.total_in > 0 && z.total_in < data.bytesize + z = nil + end + omit "deflate was not interrupted in the middle" unless z + GC.start + EnvUtil.suppress_warning { z.params(9, Zlib::HUFFMAN_ONLY) } + assert_kind_of(String, z.finish) + end; + end + def test_params_with_buf_error_and_pending_output data = Random.new(0).bytes(1000) z = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, Zlib::MAX_WBITS, 1) From 59c418366dd21197729c0e5cea1b27286f184f93 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Mon, 9 Nov 2026 16:45:41 +0900 Subject: [PATCH 2/2] Clear next_in whenever the input is kept in z->input Zlib::Inflate#sync also left next_in and avail_in pointing to the input after moving the rest of it to z->input. Clear them there too with a helper shared with zstream_run, so that next_in is always empty outside zstream_run and the pending input lives only in z->input. The adjustment of avail_in for do_inflate is no longer needed since it checks z->input. Co-Authored-By: Claude Opus 5.5 --- ext/zlib/zlib.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index f28af0a..f8e852c 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -959,6 +959,13 @@ zstream_reset_input(struct zstream *z) } } +static void +zstream_clear_next_in(struct zstream *z) +{ + z->stream.next_in = Z_NULL; + z->stream.avail_in = 0; +} + static void zstream_passthrough_input(struct zstream *z) { @@ -1201,8 +1208,7 @@ zstream_run_try(VALUE value_arg) if (state) { zstream_reset_input(z); zstream_append_input(z, z->stream.next_in, z->stream.avail_in + args->rest_in); - z->stream.next_in = Z_NULL; - z->stream.avail_in = 0; + zstream_clear_next_in(z); rb_jump_tag(state); } @@ -1244,8 +1250,7 @@ zstream_run_try(VALUE value_arg) } } } - z->stream.next_in = Z_NULL; - z->stream.avail_in = 0; + zstream_clear_next_in(z); raise_zlib_error(err, z->stream.msg); } @@ -1253,9 +1258,7 @@ zstream_run_try(VALUE value_arg) if (!NIL_P(old_input)) { rb_str_resize(old_input, 0); } - /* the rest is in z->input now; do not leave next_in in old_input */ - z->stream.next_in = Z_NULL; - z->stream.avail_in = 0; + zstream_clear_next_in(z); return Qnil; } @@ -1302,8 +1305,6 @@ zstream_inflate_sync(struct zstream *z, Bytef *src, rb_len_t len) len -= z->stream.avail_in; err = inflateSync(&z->stream); } while (err == Z_DATA_ERROR && len > 0); - /* do_inflate() looks at avail_in for the input left after the sync point */ - z->stream.avail_in = MAX_UINT(z->stream.avail_in + len); return err; } @@ -1324,9 +1325,11 @@ zstream_sync(struct zstream *z, Bytef *src, rb_len_t len) if (err == Z_OK) { zstream_discard_input(z, z->stream.next_in - (Bytef*)RSTRING_PTR(z->input)); + zstream_clear_next_in(z); zstream_append_input(z, src, len); return Qtrue; } + zstream_clear_next_in(z); zstream_reset_input(z); if (err != Z_DATA_ERROR) { /* rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); */ @@ -1339,8 +1342,10 @@ zstream_sync(struct zstream *z, Bytef *src, rb_len_t len) err = zstream_inflate_sync(z, src, len); if (err == Z_OK) { zstream_append_input(z, z->stream.next_in, src + len - z->stream.next_in); + zstream_clear_next_in(z); return Qtrue; } + zstream_clear_next_in(z); if (err != Z_DATA_ERROR) { /* rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); */ raise_zlib_error(err, z->stream.msg);