ruby-changes:47218
From: naruse <ko1@a...>
Date: Fri, 14 Jul 2017 18:18:02 +0900 (JST)
Subject: [ruby-changes:47218] naruse:r59333 (trunk): Zlib::GzipReader#pos underflows after calling #ungetbyte or #ungetc at start of file [Bug #13616]
naruse 2017-07-14 18:17:55 +0900 (Fri, 14 Jul 2017) New Revision: 59333 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59333 Log: Zlib::GzipReader#pos underflows after calling #ungetbyte or #ungetc at start of file [Bug #13616] patched by Andrew Haines <andrew@h...> [ruby-core:81488] Modified files: trunk/ext/zlib/zlib.c trunk/test/zlib/test_zlib.rb Index: test/zlib/test_zlib.rb =================================================================== --- test/zlib/test_zlib.rb (revision 59332) +++ test/zlib/test_zlib.rb (revision 59333) @@ -663,6 +663,18 @@ if defined? Zlib https://github.com/ruby/ruby/blob/trunk/test/zlib/test_zlib.rb#L663 } end + def test_ungetc_at_start_of_file + s = "".dup + w = Zlib::GzipWriter.new(StringIO.new(s)) + w << "abc" + w.close + r = Zlib::GzipReader.new(StringIO.new(s)) + + r.ungetc ?! + + assert_equal(-1, r.pos, "[ruby-core:81488][Bug #13616]") + end + def test_open Tempfile.create("test_zlib_gzip_reader_open") {|t| t.close Index: ext/zlib/zlib.c =================================================================== --- ext/zlib/zlib.c (revision 59332) +++ ext/zlib/zlib.c (revision 59333) @@ -3393,7 +3393,14 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L3393 rb_gzfile_total_out(VALUE obj) { struct gzfile *gz = get_gzfile(obj); - return rb_uint2inum(gz->z.stream.total_out - ZSTREAM_BUF_FILLED(&gz->z)); + uLong total_out = gz->z.stream.total_out; + long buf_filled = ZSTREAM_BUF_FILLED(&gz->z); + + if (total_out >= (uLong)buf_filled) { + return rb_uint2inum(total_out - buf_filled); + } else { + return LONG2FIX(-(buf_filled - total_out)); + } } /* -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/