diff options
author | Takashi Kokubun <[email protected]> | 2024-05-29 11:35:23 -0700 |
---|---|---|
committer | Takashi Kokubun <[email protected]> | 2024-05-29 11:35:23 -0700 |
commit | 2ae6df6d03c6d9750be559641c4c9f3b39eac62d (patch) | |
tree | 4db55259a159a8feb867112609cc6bb1a8a40f60 | |
parent | 2f4fe76eff0a8c6ab7a1d2fb845453acfc3cb206 (diff) |
merge revision(s) 9f8f32bf9f3758ba67dd2afe7e07d9eccb68bbc7: [Backport #20289]
[ruby/zlib] In Zlib::GzipReader#eof? check if we're actually at eof
Only consider it eof if we read ahead and something fills the buf.
If not, we may only have empty blocks and the footer.
Fixes https://github.com/ruby/zlib/pull/56
https://github.com/ruby/zlib/commit/437bea8003
-rw-r--r-- | ext/zlib/zlib.c | 3 | ||||
-rw-r--r-- | test/zlib/test_zlib.rb | 32 | ||||
-rw-r--r-- | version.h | 2 |
3 files changed, 36 insertions, 1 deletions
diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index dc608ee460..fe03072576 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -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; } diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index 779c583424..502ccceec5 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -1205,6 +1205,38 @@ if defined? Zlib } 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 @@ -11,7 +11,7 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 1 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 64 +#define RUBY_PATCHLEVEL 65 #include "ruby/version.h" #include "ruby/internal/abi.h" |