Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -4447,6 +4447,10 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj)
n = rslen;
for (;;) {
rb_len_t filled;
if (limit > 0 && limit < rslen) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This misses the case where fewer than rslen bytes remain at EOF. The early return in the while loop above reads rslen bytes without the limit, so gets("xyz", 1) on Zlib.gzip("ab") returns "ab" while StringIO returns "a".

n = limit;
break;
}
if (n > ZSTREAM_BUF_FILLED(&gz->z)) {
if (ZSTREAM_IS_FINISHED(&gz->z)) break;
gzfile_read_more(gz, Qnil);
Expand Down
8 changes: 8 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,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
Expand Down
Loading