Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Zlib::GzipReader#eof? check if we're actually at eof #72

Merged
merged 1 commit into from
Feb 22, 2024
Merged
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
3 changes: 3 additions & 0 deletions ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -3500,6 +3500,9 @@ static VALUE
rb_gzfile_eof_p(VALUE obj)
{
struct gzfile *gz = get_gzfile(obj);
while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) == 0) {
gzfile_read_more(gz, Qnil);
}
return GZFILE_IS_FINISHED(gz) ? Qtrue : Qfalse;
}

Expand Down
32 changes: 32 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,38 @@ def test_double_close
}
end

# Various methods of Zlib::GzipReader failed when to reading files
# just a few bytes larger than GZFILE_READ_SIZE.
def test_gzfile_read_size_boundary
Tempfile.create("test_zlib_gzip_read_size_boundary") {|t|
t.close
# NO_COMPRESSION helps with recreating the error condition.
# The error happens on compressed files too, but it's harder to reproduce.
# For example, ~12750 bytes are needed to trigger the error using __FILE__.
# We avoid this because the test file will change over time.
Zlib::GzipWriter.open(t.path, Zlib::NO_COMPRESSION) do |gz|
gz.print("\n" * 2024) # range from 2024 to 2033 triggers the error
gz.flush
end

Zlib::GzipReader.open(t.path) do |f|
f.readpartial(1024) until f.eof?
assert_raise(EOFError) { f.readpartial(1) }
end

Zlib::GzipReader.open(t.path) do |f|
f.readline until f.eof?
assert_raise(EOFError) { f.readline }
end

Zlib::GzipReader.open(t.path) do |f|
b = f.readbyte until f.eof?
f.ungetbyte(b)
f.readbyte
assert_raise(EOFError) { f.readbyte }
end
}
end
end

class TestZlibGzipWriter < Test::Unit::TestCase
Expand Down