From de66fea79c3eb1d65f41a6d9c0026916fa1606b2 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Sun, 25 Oct 2026 01:09:51 +0900 Subject: [PATCH 1/2] Fix Zlib::GzipReader#gets with a limit shorter than the separator The limit made the scanned length smaller than the separator, and the negative length was passed to memchr. Zlib::GzipReader.new(StringIO.new(Zlib.gzip("abc"))).gets("xyz", 1) # expected: "a" # actual: "abc" ([BUG] Segmentation fault with ASAN) Co-authored-by: Masamune (https://hackerone.com/masamune_) Co-Authored-By: Claude Opus 5.5 --- ext/zlib/zlib.c | 4 ++++ test/zlib/test_zlib.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 2f4fa11..87a63a4 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -4511,6 +4511,10 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj) n = rslen; for (;;) { rb_len_t filled; + if (limit > 0 && limit < rslen) { + n = limit; + break; + } if (n > ZSTREAM_BUF_FILLED(&gz->z)) { if (ZSTREAM_IS_FINISHED(&gz->z)) break; gzfile_read_more(gz, Qnil); diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index afe9529..e763b64 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -1388,6 +1388,14 @@ def test_gets2 } end + def test_gets_limit_shorter_than_separator + gz = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("abcd"))) + assert_equal("a", gz.gets("xyz", 1)) + assert_equal("bc", gz.gets("xyz", 2)) + assert_equal("d", gz.gets("xyz", 2)) + assert_nil(gz.gets("xyz", 2)) + end + def test_readline Tempfile.create("test_zlib_gzip_reader_readline") {|t| t.close From 58222514a63f99de3119b37c9b77b8c5ab2b6194 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 19 Nov 2026 17:31:06 +0900 Subject: [PATCH 2/2] Apply the limit of GzipReader#gets when the rest is shorter than the separator When fewer bytes than the separator remained at the end of the stream, all of them were returned regardless of the limit. Co-Authored-By: Claude Mythos 5.2 --- ext/zlib/zlib.c | 5 +++-- test/zlib/test_zlib.rb | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 87a63a4..00296b2 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -4499,10 +4499,11 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj) gzreader_skip_linebreaks(gz); } - while (ZSTREAM_BUF_FILLED(&gz->z) < rslen) { + rb_len_t minlen = (limit > 0 && limit < rslen) ? limit : rslen; + while (ZSTREAM_BUF_FILLED(&gz->z) < minlen) { if (ZSTREAM_IS_FINISHED(&gz->z)) { if (ZSTREAM_BUF_FILLED(&gz->z) > 0) gz->lineno++; - return gzfile_read(gz, rslen, Qnil); + return gzfile_read(gz, minlen, Qnil); } gzfile_read_more(gz, Qnil); } diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index e763b64..4ed4dc6 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -1394,6 +1394,11 @@ def test_gets_limit_shorter_than_separator assert_equal("bc", gz.gets("xyz", 2)) assert_equal("d", gz.gets("xyz", 2)) assert_nil(gz.gets("xyz", 2)) + + gz = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("ab"))) + assert_equal("a", gz.gets("xyz", 1)) + assert_equal("b", gz.gets("xyz", 1)) + assert_nil(gz.gets("xyz", 1)) end def test_readline