Skip to content

Commit b287b45

Browse files
authored
Merge pull request #159 from mame/zlib-deflate-params-buffer
Allocate the output buffer before Zlib::Deflate#params
2 parents 2c972a2 + 821bca4 commit b287b45

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎ext/zlib/zlib.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,9 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy)
20022002
level = ARG_LEVEL(v_level);
20032003
strategy = ARG_STRATEGY(v_strategy);
20042004

2005+
if (NIL_P(z->buf) || z->stream.avail_out == 0) {
2006+
zstream_expand_buffer(z);
2007+
}
20052008
n = z->stream.avail_out;
20062009
err = deflateParams(&z->stream, level, strategy);
20072010
filled = n - z->stream.avail_out;

‎test/zlib/test_zlib.rb‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ def test_params
267267
z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
268268
end
269269

270+
def test_params_without_output_buffer
271+
z = Zlib::Deflate.new
272+
z.params(Zlib::BEST_SPEED, Zlib::DEFAULT_STRATEGY)
273+
z << "foo"
274+
assert_equal("foo", Zlib::Inflate.inflate(z.finish))
275+
276+
z = Zlib::Deflate.new
277+
z << "foo"
278+
s = z.flush_next_out
279+
z.params(Zlib::BEST_COMPRESSION, Zlib::FILTERED)
280+
z << "bar"
281+
assert_equal("foobar", Zlib::Inflate.inflate(s + z.finish))
282+
end
283+
270284
def test_set_dictionary
271285
z = Zlib::Deflate.new
272286
z.set_dictionary("foo")

0 commit comments

Comments
 (0)