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