From 2dcdfe76d02fed91b71e5b49b6028c6c82e10d4c Mon Sep 17 00:00:00 2001 From: Gabriel Garcia Date: Mon, 6 Apr 2015 13:59:43 -0400 Subject: [PATCH] [System] Fix DeflateStream throw on empty flush, double flush. Fixes #28777 --- .../DeflateStreamTest.cs | 23 +++++++++++++++++++ support/zlib-helper.c | 4 +++- 2 files changed, 26 insertions(+), 1 deletion(-) 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; -- 2.25.1