Skip to content

Commit 110f98d

Browse files
mameclaude
andcommitted
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 <noreply@anthropic.com>
1 parent e5e71b8 commit 110f98d

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

‎ext/zlib/zlib.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4499,10 +4499,11 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
44994499
gzreader_skip_linebreaks(gz);
45004500
}
45014501

4502-
while (ZSTREAM_BUF_FILLED(&gz->z) < rslen) {
4502+
rb_len_t minlen = (limit > 0 && limit < rslen) ? limit : rslen;
4503+
while (ZSTREAM_BUF_FILLED(&gz->z) < minlen) {
45034504
if (ZSTREAM_IS_FINISHED(&gz->z)) {
45044505
if (ZSTREAM_BUF_FILLED(&gz->z) > 0) gz->lineno++;
4505-
return gzfile_read(gz, rslen, Qnil);
4506+
return gzfile_read(gz, minlen, Qnil);
45064507
}
45074508
gzfile_read_more(gz, Qnil);
45084509
}

‎test/zlib/test_zlib.rb‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,11 @@ def test_gets_limit_shorter_than_separator
13941394
assert_equal("bc", gz.gets("xyz", 2))
13951395
assert_equal("d", gz.gets("xyz", 2))
13961396
assert_nil(gz.gets("xyz", 2))
1397+
1398+
gz = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("ab")))
1399+
assert_equal("a", gz.gets("xyz", 1))
1400+
assert_equal("b", gz.gets("xyz", 1))
1401+
assert_nil(gz.gets("xyz", 1))
13971402
end
13981403

13991404
def test_readline

0 commit comments

Comments
 (0)