From 7c9c2bc1dd470cd4794d6891b21ee1fc61f401e0 Mon Sep 17 00:00:00 2001 From: naruse Date: Fri, 14 Jul 2017 09:17:55 +0000 Subject: Zlib::GzipReader#pos underflows after calling #ungetbyte or #ungetc at start of file [Bug #13616] patched by Andrew Haines [ruby-core:81488] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59333 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/zlib/zlib.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'ext/zlib') diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index bc1b47c221..ce1139dc5d 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -3393,7 +3393,14 @@ static VALUE 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)); + } } /* -- cgit v1.2.3