Skip to content

Commit 821bca4

Browse files
mameclaude
andcommitted
Allocate the output buffer before Zlib::Deflate#params
deflateParams may write output, but the buffer could be nil, and then next_out was NULL and z->buf was used as a String. Zlib::Deflate.new.params(1, 0) #=> [BUG] Segmentation fault at 0x0000000000000014 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ccb2d02 commit 821bca4

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
@@ -1947,6 +1947,9 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy)
19471947
level = ARG_LEVEL(v_level);
19481948
strategy = ARG_STRATEGY(v_strategy);
19491949

1950+
if (NIL_P(z->buf) || z->stream.avail_out == 0) {
1951+
zstream_expand_buffer(z);
1952+
}
19501953
n = z->stream.avail_out;
19511954
err = deflateParams(&z->stream, level, strategy);
19521955
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
@@ -216,6 +216,20 @@ def test_params
216216
z.close # without this, outputs `zlib(finalizer): the stream was freed prematurely.'
217217
end
218218

219+
def test_params_without_output_buffer
220+
z = Zlib::Deflate.new
221+
z.params(Zlib::BEST_SPEED, Zlib::DEFAULT_STRATEGY)
222+
z << "foo"
223+
assert_equal("foo", Zlib::Inflate.inflate(z.finish))
224+
225+
z = Zlib::Deflate.new
226+
z << "foo"
227+
s = z.flush_next_out
228+
z.params(Zlib::BEST_COMPRESSION, Zlib::FILTERED)
229+
z << "bar"
230+
assert_equal("foobar", Zlib::Inflate.inflate(s + z.finish))
231+
end
232+
219233
def test_set_dictionary
220234
z = Zlib::Deflate.new
221235
z.set_dictionary("foo")

0 commit comments

Comments
 (0)