[System] Fix DeflateStream throw on empty flush, double flush. Fixes #28777
authorGabriel Garcia <garciat@live.com>
Mon, 6 Apr 2015 17:59:43 +0000 (13:59 -0400)
committerGabriel Garcia <garciat@live.com>
Tue, 7 Apr 2015 10:22:32 +0000 (06:22 -0400)
mcs/class/System/Test/System.IO.Compression/DeflateStreamTest.cs
support/zlib-helper.c

index 3a0c7d89e5816ad949edc7f6a32e57d643f7efe2..f3cefe8c1513aebc1e7095aad330388f7d960011 100644 (file)
@@ -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 ();
+               }
        }
 }
 
index 83455413b3647915ae8cc3cd0ac8e025cf5823be..38696260a092fb4b509f7eabc7f9fef96032b2b8 100644 (file)
@@ -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;