Skip to content

Commit 7227fdf

Browse files
authored
Merge pull request #180 from segiddins/segiddins/gzreader-gets-separator-char-boundary
Do not match the line separator inside a multibyte character in GzipReader#gets
2 parents d828cf4 + 92a8701 commit 7227fdf

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎ext/zlib/zlib.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4560,6 +4560,12 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
45604560
else {
45614561
n += (rb_len_t)(res - p);
45624562
p = res;
4563+
if (maxlen > 1 &&
4564+
rb_enc_left_char_head(RSTRING_PTR(gz->z.buf), p, RSTRING_PTR(gz->z.buf) + ZSTREAM_BUF_FILLED(&gz->z), enc) != p) {
4565+
/* the match starts inside a multibyte character: keep searching */
4566+
p++, n++;
4567+
continue;
4568+
}
45634569
if (rslen == 1 || memcmp(p, rsptr, rslen) == 0) break;
45644570
p++, n++;
45654571
}

‎test/zlib/test_zlib.rb‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,20 @@ def test_reader_wrap
15281528
}
15291529
end
15301530

1531+
def test_gets_separator_inside_multibyte_char
1532+
pend "rb_io_extract_encoding_option ignores the encoding options on TruffleRuby" if RUBY_ENGINE == 'truffleruby'
1533+
# In Shift_JIS the second byte of a character can be an ASCII byte such
1534+
# as "@"; a separator must only match on a character boundary.
1535+
content = "\x81\x40?\x81\x40@".b
1536+
r = Zlib::GzipReader.new(StringIO.new(Zlib.gzip(content)), external_encoding: "Shift_JIS")
1537+
assert_equal(content.dup.force_encoding("Shift_JIS"), r.gets("@"))
1538+
assert_nil(r.gets("@"))
1539+
r = Zlib::GzipReader.new(StringIO.new(Zlib.gzip(content)), external_encoding: "Shift_JIS")
1540+
assert_equal("\x81\x40?".b.force_encoding("Shift_JIS"), r.gets("?"))
1541+
assert_equal("\x81\x40@".b.force_encoding("Shift_JIS"), r.gets("?"))
1542+
assert_nil(r.gets("?"))
1543+
end
1544+
15311545
def test_corrupted_header
15321546
gz = Zlib::GzipWriter.new(StringIO.new(s = "".dup))
15331547
gz.orig_name = "X"

0 commit comments

Comments
 (0)