Skip to content

Commit b1f182e

Browse files
committed
Fix a bug that GZipReader#gets may return incomplete line
See also: ruby/csv#117 (comment) How to reproduce with x.csv.gz in the issue comment: Zlib::GzipReader.open("x.csv.gz") do |rio| rio.gets(nil, 1024) while line = rio.gets(nil, 8192) raise line unless line.valid_encoding? end end Reported by Dimitrij Denissenko. Thanks!!!
1 parent dd593ac commit b1f182e

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

ext/zlib/zlib.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4199,17 +4199,17 @@ gzreader_charboundary(struct gzfile *gz, long n)
41994199
{
42004200
char *s = RSTRING_PTR(gz->z.buf);
42014201
char *e = s + ZSTREAM_BUF_FILLED(&gz->z);
4202-
char *p = rb_enc_left_char_head(s, s + n, e, gz->enc);
4202+
char *p = rb_enc_left_char_head(s, s + n - 1, e, gz->enc);
42034203
long l = p - s;
42044204
if (l < n) {
4205-
n = rb_enc_precise_mbclen(p, e, gz->enc);
4206-
if (MBCLEN_NEEDMORE_P(n)) {
4207-
if ((l = gzfile_fill(gz, l + MBCLEN_NEEDMORE_LEN(n))) > 0) {
4205+
int n_bytes = rb_enc_precise_mbclen(p, e, gz->enc);
4206+
if (MBCLEN_NEEDMORE_P(n_bytes)) {
4207+
if ((l = gzfile_fill(gz, n + MBCLEN_NEEDMORE_LEN(n_bytes))) > 0) {
42084208
return l;
42094209
}
42104210
}
4211-
else if (MBCLEN_CHARFOUND_P(n)) {
4212-
return l + MBCLEN_CHARFOUND_LEN(n);
4211+
else if (MBCLEN_CHARFOUND_P(n_bytes)) {
4212+
return l + MBCLEN_CHARFOUND_LEN(n_bytes);
42134213
}
42144214
}
42154215
return n;

0 commit comments

Comments
 (0)