Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down