Skip to content

Commit ccb2d02

Browse files
mameclaude
andcommitted
Carry over pending output when the inflate buffer is replaced
In the block form, output not yet yielded stays in the buffer between calls. It was silently dropped when another buffer (or none) was given for the next call. data = Zlib::Deflate.deflate("A" * 50000) half = data.bytesize / 2 inf = Zlib::Inflate.new out = +"" inf.inflate(data[0, half], buffer: String.new) { |c| out << c } inf.inflate(data[half..], buffer: String.new) { |c| out << c } out << inf.finish out.bytesize # expected: 50000 # actual: 47806 Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
1 parent ed296aa commit ccb2d02

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎ext/zlib/zlib.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,14 +2202,24 @@ rb_inflate_inflate_body(VALUE _arguments)
22022202
rb_str_modify_expand(buffer, len);
22032203
}
22042204
rb_str_set_len(buffer, 0);
2205+
if (ZSTREAM_REUSE_BUFFER_P(z) && !NIL_P(z->buf) && RSTRING_LEN(z->buf) > 0) {
2206+
/* carry over the output pending in the previous buffer */
2207+
rb_str_buf_cat(buffer, RSTRING_PTR(z->buf), RSTRING_LEN(z->buf));
2208+
}
22052209
z->flags |= ZSTREAM_REUSE_BUFFER;
22062210
z->buf = buffer;
22072211
}
22082212
} else if (ZSTREAM_REUSE_BUFFER_P(z)) {
2213+
VALUE pending = z->buf;
22092214
z->flags &= ~ZSTREAM_REUSE_BUFFER;
22102215
z->buf = Qnil;
22112216
z->stream.next_out = NULL;
22122217
z->stream.avail_out = 0;
2218+
if (RSTRING_LEN(pending) > 0) {
2219+
/* carry over the output pending in the previous buffer */
2220+
zstream_append_buffer2(z, pending);
2221+
}
2222+
RB_GC_GUARD(pending);
22132223
}
22142224
rb_scan_args(argc, argv, "10", &src);
22152225

‎test/zlib/test_zlib.rb‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,45 @@ def test_inflate_buffer_modified_between_calls
472472
assert_match(/\A0*\z/, inflated)
473473
end
474474

475+
def test_inflate_buffer_replaced_between_calls
476+
# Output pending in the previous buffer is carried over when another
477+
# buffer (or no buffer) is given for the next call.
478+
src = "0" * 100_000
479+
deflated = Zlib::Deflate.deflate(src)
480+
481+
# block form, a different buffer object for each call
482+
z = Zlib::Inflate.new
483+
inflated = "".dup
484+
(0...deflated.bytesize).step(200) do |i|
485+
z.inflate(deflated.byteslice(i, 200), buffer: String.new(capacity: 1 << 14)) { |chunk| inflated << chunk }
486+
end
487+
z.inflate(nil, buffer: String.new) { |chunk| inflated << chunk }
488+
assert_equal src, inflated
489+
490+
# block form, buffer: dropped mid-stream
491+
z = Zlib::Inflate.new
492+
buf = String.new(capacity: 1 << 14)
493+
inflated = "".dup
494+
half = deflated.bytesize / 2
495+
z.inflate(deflated.byteslice(0, half), buffer: buf) { |chunk| inflated << chunk }
496+
z.inflate(deflated.byteslice(half..)) { |chunk| inflated << chunk }
497+
inflated << z.finish
498+
assert_equal src, inflated
499+
500+
# NeedDict handled with a different buffer object, with GC in between
501+
dict = "dictionary-" * 4
502+
d = Zlib::Deflate.new
503+
d.set_dictionary(dict)
504+
deflated = d.deflate("A" * 12_000, Zlib::FINISH)
505+
z = Zlib::Inflate.new
506+
head, rest = deflated.byteslice(0, 6), deflated.byteslice(6..)
507+
assert_raise(Zlib::NeedDict) { z.inflate(head, buffer: String.new(capacity: 1 << 14)) }
508+
z.set_dictionary(dict)
509+
EnvUtil.under_gc_stress do
510+
assert_equal "A" * 12_000, z.inflate(rest, buffer: String.new(capacity: 1 << 14))
511+
end
512+
end
513+
475514
def test_inflate_buffer_then_addstr_and_finish
476515
src = "A" * 50_000
477516
deflated = Zlib::Deflate.deflate(src)

0 commit comments

Comments
 (0)