From 144466a1be9eaac07d13714c68c69c72561d27c6 Mon Sep 17 00:00:00 2001 From: Aristide CORDONNIER Date: Thu, 12 Nov 2026 19:13:42 -0600 Subject: [PATCH] Do not write through a shared buffer in GzipReader#read and #readpartial zstream_shift_buffer() fills the buffer given by the caller with rb_str_resize() followed by memcpy(). rb_str_resize() returns at once for a string that shares its memory when the length does not change, and it only clears the coderange when the string shrinks. So when the buffer still shared its memory with a copy (String#dup, a frozen copy, the copy a Hash makes of its key), the next read of the same length overwrote that copy, and the buffer kept the coderange of its previous contents. gz = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("A" * 1000 + "B" * 1000))) buf = "".dup copy = gz.readpartial(1000, buf).dup gz.readpartial(1000, buf) copy[0, 4] # => "BBBB" Call rb_str_modify() on the buffer before writing into it. Co-Authored-By: Claude Mythos 5.2 --- ext/zlib/zlib.c | 4 ++++ test/zlib/test_zlib.rb | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index e279bb9..1db203d 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -841,6 +841,9 @@ zstream_shift_buffer(struct zstream *z, rb_len_t len, VALUE dst) } else { bufptr = RSTRING_PTR(z->buf); rb_str_resize(dst, buflen); + /* dst may still share its memory with other strings, and + * its coderange describes the old contents */ + rb_str_modify(dst); memcpy(RSTRING_PTR(dst), bufptr, buflen); } buflen = 0; @@ -850,6 +853,7 @@ zstream_shift_buffer(struct zstream *z, rb_len_t len, VALUE dst) dst = rb_str_new(bufptr, len); } else { rb_str_resize(dst, len); + rb_str_modify(dst); memcpy(RSTRING_PTR(dst), bufptr, len); } buflen -= len; diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index afe9529..f2e3c44 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -1286,6 +1286,41 @@ def test_readpartial } end + def test_outbuf_shared + # A copy of a long string shares its memory with the original + # until one of them is modified. Reading into the buffer again + # must not write through that shared memory. + a = "A" * 1000 + b = "B" * 1000 + gz = Zlib.gzip(a + b) + %i[read readpartial].each do |m| + Zlib::GzipReader.wrap(StringIO.new(gz)) do |f| + buf = "".dup + copy = f.__send__(m, 1000, buf).dup + frozen = copy.dup.freeze + key = {buf => true}.keys.first + assert_same buf, f.__send__(m, 1000, buf) + assert_equal b, buf, m + assert_equal a, copy, m + assert_equal a, frozen, m + assert_equal a, key, m + end + end + end + + def test_outbuf_coderange + gz = Zlib.gzip("\xFF".b * 10) + %i[read readpartial].each do |m| + Zlib::GzipReader.wrap(StringIO.new(gz)) do |f| + buf = "".dup + assert_predicate buf, :ascii_only? + f.__send__(m, 10, buf) + assert_equal "\xFF".b * 10, buf.b, m + assert_not_predicate buf, :ascii_only?, m + end + end + end + def test_getc Tempfile.create("test_zlib_gzip_reader_getc") {|t| t.close