From d5e96c9f407bcb04e910c7dadf159e5974af17d2 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Sun, 25 Oct 2026 00:34:53 +0900 Subject: [PATCH 1/2] Raise InProgressError from other methods while the stream is running Only #inflate, #deflate and the methods that run the stream checked ZSTREAM_IN_PROGRESS. The others could change the zlib state or the output buffer while another thread was running zlib without the GVL, or from the block of #inflate/#deflate. zd = Zlib::Deflate.new Thread.new { sleep 0.1; zd.close } zd.deflate(Random.bytes(50_000_000), Zlib::FINISH) #=> [BUG] Segmentation fault at 0x0000000000000000 Co-Authored-By: Claude Opus 5.5 --- ext/zlib/zlib.c | 46 ++++++++++++++++++++++++++++++++++-------- test/zlib/test_zlib.rb | 34 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 3569633..6ae15e8 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -169,6 +169,7 @@ static VALUE rb_inflate_addstr(VALUE, VALUE); static VALUE rb_inflate_sync(VALUE, VALUE); static VALUE rb_inflate_sync_point_p(VALUE); static VALUE rb_inflate_set_dictionary(VALUE, VALUE); +static void inflate_set_dictionary(struct zstream*, VALUE); #if GZIP_SUPPORT struct gzfile; @@ -1177,6 +1178,14 @@ zstream_run_func(struct zstream_run_args *args) return err; } +static void +zstream_check_not_in_progress(struct zstream *z) +{ + if (z->flags & ZSTREAM_IN_PROGRESS) { + rb_raise(cInProgressError, "zlib stream is in progress"); + } +} + static VALUE zstream_run_try(VALUE value_arg) { @@ -1256,7 +1265,7 @@ zstream_run_try(VALUE value_arg) VALUE dicts = rb_ivar_get(self, id_dictionaries); VALUE dict = rb_hash_aref(dicts, rb_uint2inum(z->stream.adler)); if (!NIL_P(dict)) { - rb_inflate_set_dictionary(self, dict); + inflate_set_dictionary(z, dict); if (z->stream.avail_in > 0) goto loop; /* the dictionary id was the last byte available; inflate() * would only report Z_BUF_ERROR now */ @@ -1309,9 +1318,7 @@ zstream_run(struct zstream *z, Bytef *src, rb_len_t len, int flush) }; /* Cannot start zstream while it is in progress. */ - if (z->flags & ZSTREAM_IN_PROGRESS) { - rb_raise(cInProgressError, "zlib stream is in progress"); - } + zstream_check_not_in_progress(z); rb_ensure(zstream_run_try, (VALUE)&args, zstream_run_ensure, (VALUE)&args); if (args.jump_state) @@ -1516,7 +1523,10 @@ get_zstream(VALUE obj) static VALUE rb_zstream_end(VALUE obj) { - zstream_end(get_zstream(obj)); + struct zstream *z = get_zstream(obj); + + zstream_check_not_in_progress(z); + zstream_end(z); return Qnil; } @@ -1527,7 +1537,10 @@ rb_zstream_end(VALUE obj) static VALUE rb_zstream_reset(VALUE obj) { - zstream_reset(get_zstream(obj)); + struct zstream *z = get_zstream(obj); + + zstream_check_not_in_progress(z); + zstream_reset(z); return Qnil; } @@ -1563,6 +1576,7 @@ rb_zstream_flush_next_in(VALUE obj) VALUE dst; TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); + zstream_check_not_in_progress(z); dst = zstream_detach_input(z); return dst; } @@ -1582,6 +1596,7 @@ rb_zstream_flush_next_out(VALUE obj) struct zstream *z; TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); + zstream_check_not_in_progress(z); return zstream_detach_buffer(z); } @@ -1612,6 +1627,7 @@ rb_zstream_set_avail_out(VALUE obj, VALUE size) if (len < 0) rb_raise(rb_eArgError, "negative size"); + zstream_check_not_in_progress(z); zstream_expand_buffer_into(z, len); return size; } @@ -1821,6 +1837,7 @@ rb_deflate_init_copy(VALUE self, VALUE orig) z2 = get_zstream(orig); if (z1 == z2) return self; + zstream_check_not_in_progress(z2); err = deflateCopy(&z1->stream, &z2->stream); if (err != Z_OK) { raise_zlib_error(err, 0); @@ -2032,6 +2049,7 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy) level = ARG_LEVEL(v_level); strategy = ARG_STRATEGY(v_strategy); + zstream_check_not_in_progress(z); if (NIL_P(z->buf) || z->stream.avail_out == 0) { zstream_expand_buffer(z); @@ -2077,6 +2095,7 @@ rb_deflate_set_dictionary(VALUE obj, VALUE dic) int err; StringValue(src); + zstream_check_not_in_progress(z); err = deflateSetDictionary(&z->stream, (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src)); if (err != Z_OK) { @@ -2443,6 +2462,7 @@ rb_inflate_sync(VALUE obj, VALUE src) struct zstream *z = get_zstream(obj); StringValue(src); + zstream_check_not_in_progress(z); return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src)); } @@ -2459,6 +2479,7 @@ rb_inflate_sync_point_p(VALUE obj) struct zstream *z = get_zstream(obj); int err; + zstream_check_not_in_progress(z); err = inflateSyncPoint(&z->stream); if (err == 1) { return Qtrue; @@ -2481,6 +2502,17 @@ rb_inflate_set_dictionary(VALUE obj, VALUE dic) { struct zstream *z = get_zstream(obj); VALUE src = dic; + + StringValue(src); + zstream_check_not_in_progress(z); + inflate_set_dictionary(z, src); + + return dic; +} + +static void +inflate_set_dictionary(struct zstream *z, VALUE src) +{ int err; StringValue(src); @@ -2489,8 +2521,6 @@ rb_inflate_set_dictionary(VALUE obj, VALUE dic) if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } - - return dic; } diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index 988a271..2fbda24 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -970,6 +970,40 @@ def test_in_progress_error_does_not_end_in_progress zd.finish end + def test_deflate_in_progress + zd = Zlib::Deflate.new + s = Random.new(0).bytes(100_000) + zd.deflate(s, Zlib::FINISH) do + assert_raise(Zlib::InProgressError) { zd.close } + assert_raise(Zlib::InProgressError) { zd.reset } + assert_raise(Zlib::InProgressError) { zd.flush_next_in } + assert_raise(Zlib::InProgressError) { zd.flush_next_out } + assert_raise(Zlib::InProgressError) { zd.avail_out = 10 } + assert_raise(Zlib::InProgressError) { zd.params(1, Zlib::DEFAULT_STRATEGY) } + assert_raise(Zlib::InProgressError) { zd.set_dictionary("foo") } + assert_raise(Zlib::InProgressError) { zd.dup } + break + end + zd.finish + end + + def test_inflate_in_progress + zi = Zlib::Inflate.new + s = Zlib.deflate(Random.new(0).bytes(100_000)) + zi.inflate(s) do + assert_raise(Zlib::InProgressError) { zi.close } + assert_raise(Zlib::InProgressError) { zi.reset } + assert_raise(Zlib::InProgressError) { zi.flush_next_in } + assert_raise(Zlib::InProgressError) { zi.flush_next_out } + assert_raise(Zlib::InProgressError) { zi.avail_out = 10 } + assert_raise(Zlib::InProgressError) { zi.sync("foo") } + assert_raise(Zlib::InProgressError) { zi.sync_point? } + assert_raise(Zlib::InProgressError) { zi.set_dictionary("foo") } + break + end + zi.close + end + def test_recursive_inflate original_gc_stress = GC.stress GC.stress = true From 4daca177ac0af7f7df472219b82df3a6eed03fe1 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Sun, 25 Oct 2026 01:03:04 +0900 Subject: [PATCH 2/2] Raise InProgressError from GzipFile methods while the stream is running GzipFile methods did not check ZSTREAM_IN_PROGRESS either, so another thread could close the stream or replace its buffer while zlib was running without the GVL. gz = Zlib::GzipWriter.new(StringIO.new) q = Thread::Queue.new Thread.new { q.pop; gz.close } gz.write("x") q << true gz.write(Random.bytes(10_000_000)) #=> can't modify frozen NilClass: nil (FrozenError) Co-Authored-By: Claude Opus 5.5 --- ext/zlib/zlib.c | 3 +++ test/zlib/test_zlib.rb | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 6ae15e8..66703dd 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -3358,6 +3358,7 @@ get_gzfile(VALUE obj) if (!ZSTREAM_IS_READY(&gz->z)) { rb_raise(cGzError, "closed gzip stream"); } + zstream_check_not_in_progress(&gz->z); return gz; } @@ -3705,6 +3706,7 @@ rb_gzfile_close(VALUE obj) if (!ZSTREAM_IS_READY(&gz->z)) { return Qnil; } + zstream_check_not_in_progress(&gz->z); io = gz->io; gzfile_close(gz, 1); return io; @@ -4251,6 +4253,7 @@ rb_gzreader_unused(VALUE obj) { struct gzfile *gz; TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); + zstream_check_not_in_progress(&gz->z); return gzfile_reader_get_unused(gz); } diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index 2fbda24..0828de4 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -1966,6 +1966,27 @@ def test_thread_wakeup_interrupt end class TestZlibGzipWriter < Test::Unit::TestCase + def test_close_while_writing_in_another_thread + gz = Zlib::GzipWriter.new(StringIO.new(+"")) + s = Random.new(0).bytes(1_000_000) + q = Thread::Queue.new + th = Thread.new do + gz.write(s) + q << true + 10.times { gz.write(s) } + rescue Zlib::GzipFile::Error, Zlib::InProgressError + end + q.pop + begin + gz.close + rescue Zlib::InProgressError + Thread.pass + retry + end + th.join + assert_predicate(gz, :closed?) + end + def test_invalid_new assert_raise(NoMethodError, "[ruby-dev:23228]") { Zlib::GzipWriter.new(nil).close } assert_raise(NoMethodError, "[ruby-dev:23344]") { Zlib::GzipWriter.new(true).close }