[前][次][番号順一覧][スレッド一覧]

ruby-changes:68476

From: Sutou <ko1@a...>
Date: Fri, 15 Oct 2021 15:31:30 +0900 (JST)
Subject: [ruby-changes:68476] 027a3379d6 (master): [ruby/zlib] Fix a bug that GZipReader#gets may return incomplete line

https://git.ruby-lang.org/ruby.git/commit/?id=027a3379d6

From 027a3379d67922738d503511c2123989229f8d9b Mon Sep 17 00:00:00 2001
From: Sutou Kouhei <kou@c...>
Date: Tue, 5 Oct 2021 16:43:21 +0900
Subject: [ruby/zlib] Fix a bug that GZipReader#gets may return incomplete line

See also: https://github.com/ruby/csv/issues/117#issuecomment-933289373

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!!!

https://github.com/ruby/zlib/commit/b1f182e98f
---
 ext/zlib/zlib.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c
index 147b7344db..fb7a5d5ae3 100644
--- a/ext/zlib/zlib.c
+++ b/ext/zlib/zlib.c
@@ -4199,17 +4199,17 @@ gzreader_charboundary(struct gzfile *gz, long n) https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L4199
 {
     char *s = RSTRING_PTR(gz->z.buf);
     char *e = s + ZSTREAM_BUF_FILLED(&gz->z);
-    char *p = rb_enc_left_char_head(s, s + n, e, gz->enc);
+    char *p = rb_enc_left_char_head(s, s + n - 1, e, gz->enc);
     long l = p - s;
     if (l < n) {
-	n = rb_enc_precise_mbclen(p, e, gz->enc);
-	if (MBCLEN_NEEDMORE_P(n)) {
-	    if ((l = gzfile_fill(gz, l + MBCLEN_NEEDMORE_LEN(n))) > 0) {
+	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))) > 0) {
 		return l;
 	    }
 	}
-	else if (MBCLEN_CHARFOUND_P(n)) {
-	    return l + MBCLEN_CHARFOUND_LEN(n);
+	else if (MBCLEN_CHARFOUND_P(n_bytes)) {
+	    return l + MBCLEN_CHARFOUND_LEN(n_bytes);
 	}
     }
     return n;
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]