From 082197d5c8f618d1cf78dffd199915806079b7de Mon Sep 17 00:00:00 2001
From: dkf <donal.k.fellows@manchester.ac.uk>
Date: Mon, 4 Apr 2016 10:03:40 +0000
Subject: Was handling the flushing at the end of the stream wrongly.

---
 generic/tclZlib.c | 9 +++++----
 tests/zlib.test   | 8 ++++----
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/generic/tclZlib.c b/generic/tclZlib.c
index 50d9a30..691d57a 100644
--- a/generic/tclZlib.c
+++ b/generic/tclZlib.c
@@ -1194,11 +1194,12 @@ Tcl_ZlibStreamPut(
 	zshPtr->stream.next_out = (Bytef *) dataTmp;
 
 	e = deflate(&zshPtr->stream, flush);
-	while (e == Z_BUF_ERROR) {
+	while (e == Z_BUF_ERROR || (flush == Z_FINISH && e == Z_OK)) {
 	    /*
-	     * Output buffer too small to hold the data being generated; so
-	     * put a new buffer into place after saving the old generated
-	     * data to the outData list.
+	     * Output buffer too small to hold the data being generated or we
+	     * are doing the end-of-stream flush (which can spit out masses of
+	     * data). This means we need to put a new buffer into place after
+	     * saving the old generated data to the outData list.
 	     */
 
 	    obj = Tcl_NewByteArrayObj((unsigned char *) dataTmp, outSize);
diff --git a/tests/zlib.test b/tests/zlib.test
index 93c00f1..968469d 100644
--- a/tests/zlib.test
+++ b/tests/zlib.test
@@ -879,8 +879,7 @@ test zlib-11.3 {Bug 3595576 variant} -setup {
 test zlib-12.1 {Tk Bug 9eb55debc5} -constraints zlib -setup {
     set stream [zlib stream compress]
 } -body {
-    set opts {}
-    for {set y 0} {$y < 60} {incr y} {
+    for {set opts {};set y 0} {$y < 60} {incr y} {
 	for {set line {};set x 0} {$x < 100} {incr x} {
 	    append line [binary format ccc $x $y 128]
 	}
@@ -889,10 +888,11 @@ test zlib-12.1 {Tk Bug 9eb55debc5} -constraints zlib -setup {
 	}
 	$stream put {*}$opts $line
     }
-    string length [$stream get]
+    set data [$stream get]
+    list [string length $data] [string length [zlib decompress $data]]
 } -cleanup {
     $stream close
-} -result 12026
+} -result {12026 18000}
 
 ::tcltest::cleanupTests
 return
-- 
cgit v0.12