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: 3 additions & 1 deletion ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -4376,7 +4376,9 @@ gzreader_charboundary(struct gzfile *gz, rb_len_t n)
if (l < n) {
int n_bytes = rb_enc_precise_mbclen(p, e, gz->enc);
if (MBCLEN_NEEDMORE_P(n_bytes)) {
if ((l = gzfile_fill(gz, n + MBCLEN_NEEDMORE_LEN(n_bytes), Qnil)) > 0) {
/* the character needs MBCLEN_NEEDMORE_LEN bytes beyond the END of the
* buffer, which may lie past n */
if ((l = gzfile_fill(gz, ZSTREAM_BUF_FILLED(&gz->z) + MBCLEN_NEEDMORE_LEN(n_bytes), Qnil)) > 0) {
return l;
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,22 @@ def test_reader_wrap
}
end

def test_gets_limit_multibyte_boundary
# The inflated buffer ends in the middle of a character whenever a
# 2048-byte read of compressed data does; gets(limit) must still return
# whole characters (same as IO#gets(limit)).
content = (("\u3042\u3044\u3046" * 700) + "\n") * 3
[1, 4, 7, 10].each do |limit|
r = Zlib::GzipReader.new(StringIO.new(Zlib.gzip(content, level: 0)))
lines = []
while (line = r.gets(limit))
lines << line
end
assert_equal(content, lines.join)
assert_empty(lines.reject(&:valid_encoding?), "limit #{limit}")
end
end

def test_corrupted_header
gz = Zlib::GzipWriter.new(StringIO.new(s = "".dup))
gz.orig_name = "X"
Expand Down
Loading