From b2b9419cc254d492dffc25e51420979884e71fda Mon Sep 17 00:00:00 2001 From: Fletcher Dares Date: Thu, 19 Nov 2026 19:47:47 -0500 Subject: [PATCH] patch seg fault in Zlib::Deflate.new.params(1, Zlib::FILTERED) --- ext/zlib/zlib.c | 11 ++++++++--- test/zlib/test_zlib.rb | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 2f4fa11..58cf443 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -1975,13 +1975,14 @@ rb_deflate_flush(int argc, VALUE *argv, VALUE obj) } /* - * Document-method: Zlib::Deflate.params + * Document-method: Zlib::Deflate#params * - * call-seq: params(level, strategy) + * call-seq: params(level, strategy) -> nil * * Changes the parameters of the deflate stream to allow changes between * different types of data that require different types of compression. Any - * unprocessed data is flushed before changing the params. + * unprocessed data is flushed first. You can also call this method before + * supplying any input. * * See Zlib::Deflate.new for a description of +level+ and +strategy+. * @@ -1998,6 +1999,10 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy) level = ARG_LEVEL(v_level); strategy = ARG_STRATEGY(v_strategy); + if (NIL_P(z->buf)) { + zstream_expand_buffer(z); + } + n = z->stream.avail_out; err = deflateParams(&z->stream, level, strategy); filled = n - z->stream.avail_out; diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index afe9529..c5de17c 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -236,6 +236,15 @@ def test_params_with_buf_error_and_pending_output assert_equal(data, Zlib::Inflate.inflate(z.finish)) end + def test_params_before_input + z = Zlib::Deflate.new + assert_nil(z.params(Zlib::BEST_SPEED, Zlib::FILTERED)) + z << "foo" + assert_equal("foo", Zlib::Inflate.inflate(z.finish)) + ensure + z&.close + end + def test_params z = Zlib::Deflate.new z << "foo"