diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 187e358..5bd4003 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -1945,7 +1945,8 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy) if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } - rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled); + if (!NIL_P(z->buf)) + rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled); return Qnil; } diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index 71159a0..cc34205 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -216,6 +216,13 @@ def test_params z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.' end + def test_params_before_output + z = Zlib::Deflate.new + EnvUtil.suppress_warning {z.params(Zlib::BEST_COMPRESSION, Zlib::DEFAULT_STRATEGY)} + z << "foo" + assert_equal("foo", Zlib::Inflate.inflate(z.finish)) + end + def test_set_dictionary z = Zlib::Deflate.new z.set_dictionary("foo")