From: João Matos Date: Tue, 7 Apr 2015 16:31:12 +0000 (+0100) Subject: Merge pull request #1684 from Garciat/fix-zlib-helper X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=bcb651443de098171a4e71a71d7f21a599323a4f;hp=6c38ce4ea40d0bcacf9dfbd72d987d2760145982;p=mono.git Merge pull request #1684 from Garciat/fix-zlib-helper [System] Fix DeflateStream throw on empty flush, double flush. Fixes #28777 --- diff --git a/mcs/class/System/Test/System.IO.Compression/DeflateStreamTest.cs b/mcs/class/System/Test/System.IO.Compression/DeflateStreamTest.cs index 3a0c7d89e58..f3cefe8c151 100644 --- a/mcs/class/System/Test/System.IO.Compression/DeflateStreamTest.cs +++ b/mcs/class/System/Test/System.IO.Compression/DeflateStreamTest.cs @@ -374,6 +374,29 @@ namespace MonoTests.System.IO.Compression compressing.Close (); backing.Close (); } + + [Test] + public void Bug28777_EmptyFlush () + { + MemoryStream backing = new MemoryStream (); + DeflateStream compressing = new DeflateStream (backing, CompressionLevel.Fastest, true); + compressing.Flush (); + compressing.Close (); + backing.Close (); + } + + [Test] + public void Bug28777_DoubleFlush () + { + byte[] buffer = new byte [4096]; + MemoryStream backing = new MemoryStream (); + DeflateStream compressing = new DeflateStream (backing, CompressionLevel.Fastest, true); + compressing.Write (buffer, 0, buffer.Length); + compressing.Flush (); + compressing.Flush (); + compressing.Close (); + backing.Close (); + } } } diff --git a/support/zlib-helper.c b/support/zlib-helper.c index 83455413b36..38696260a09 100644 --- a/support/zlib-helper.c +++ b/support/zlib-helper.c @@ -90,6 +90,8 @@ CreateZStream (gint compress, guchar gzip, read_write_func func, void *gchandle) result->gchandle = gchandle; result->compress = compress; result->buffer = g_new (guchar, BUFFER_SIZE); + result->stream->next_out = result->buffer; + result->stream->avail_out = BUFFER_SIZE; return result; } @@ -148,7 +150,7 @@ flush_internal (ZStream *stream, gboolean is_final) if (!stream->compress) return 0; - if (!is_final) { + if (!is_final && stream->stream->avail_in != 0) { status = deflate (stream->stream, Z_PARTIAL_FLUSH); if (status != Z_OK && status != Z_STREAM_END) return status;