zlib transform issue, multi-stream inflate ignores streams after first
With that issue fixed the following scenario was tried:
- `cat example.deflated example.deflated > example.multi-stream`. The result file contains two separate zlib deflate streams.
- Modified example.tcl to read this file, and to attempt extraction of both streams.
proc inflate-transform {a b} {
set a [open $a rb]
set b [open $b wb]
puts I/O.0:[tell $a]/[tell $b]
zlib push inflate $a
puts C:[fconfigure $a]
fcopy $a $b
puts I/O.1:[tell $a]/[tell $b]
chan pop $a
puts I/O.2:[tell $a]/[tell $b]
zlib push inflate $a
fcopy $a $b
puts I/O.3:[tell $a]/[tell $b]
chan pop $a
puts I/O.4:[tell $a]/[tell $b]
close $a
close $b
return
}
The result:
- First stream extracted ok
- Second stream was ignored (skipped over when reading the first stream).
Investigation of `ZlibTransformInput` and its subordinate `ResultGenerate` shows that while `RG` properly detects the stream end this condition is not signaled to its parent `ZTI`, which proceeds to continue reading from the underlying stream until it hits the underlying EOF, while never writing more to the output. IOW it effectively skips all data after the first stream.
After the extraction of the first stream the underlying channel is at its EOF, and the second stream cannot be extracted anymore.
The patch
Index: generic/tclZlib.c
==================================================================
--- generic/tclZlib.c
+++ generic/tclZlib.c
@@ -126,10 +126,12 @@
Tcl_TimerToken timer; /* Timer used for keeping events fresh. */
Tcl_DString decompressed; /* Buffer for decompression results. */
Tcl_Obj *compDictObj; /* Byte-array object containing compression
* dictionary (not dictObj!) to use if
* necessary. */
+ int done; /* Flag to signal stream end up to transform
+ * input */
} ZlibChannelData;
/*
* Value bits for the flags field. Definitions are:
- ASYNC - Whether this is an asynchronous channel. @@ -3011,10 +3013,14 @@ gotBytes += copied;
if (toRead == 0) { return gotBytes; } + + if (cd->done) { + return gotBytes; + }
/* * The buffer is exhausted, but the caller wants even more. We now * have to go to the underlying channel, get more bytes and then * transform them for delivery. We may not get what we want (full EOF @@ -3831,10 +3837,13 @@ */
if (((flush == Z_SYNC_FLUSH) && (e == Z_BUF_ERROR)) || (e == Z_STREAM_END) || (e == Z_OK && written == 0)) { + if (e == Z_STREAM_END) { + cd->done = 1; + } return TCL_OK; }
/* * Z_BUF_ERROR can be ignored as per http://www.zlib.net/zlib_how.html
solved that issue. A new flag to capture the stream eof condition, setting it at the appropriate place, and a check in the caller to abort buffer filling when reaching stream end.
At this point we can then also demonstrate that setting `-limit` to a value > 1 causes the second stream to be either ignored, or to generate an error on trying to read it. It is ignored if the `-limit` was large enough to skip over the entire stream. An error happens if the 2nd stream was partially read while processing the end of the first, causing zlib to see something which is not a valid stream anymore.
Stopping here. Have to investigate now if it is possible to determine which parts of the underlying stream are read and not processed by zlib, and then if it is possible to stash that part back for the next transformation, or just the plain channel.
05f80ed1bcmerge zlib-chan-f70ce1fead, [f70ce1fead] - rewritten and optimized inflate: default read ahead limit set to 4K (instead of 1); better SF/BO prevention; code simplification, no interim copy/move buffers, no decompression d-string, the inflate occurring in single step (ResultDecompress combines ResultGenerate and ResultCopy); better handling in non-blocking mode (also recognizes or distinguish no data/EAGAIN cases). check-in2020-06-24 09:02:01165b6516eaAdded two tests demonstrating that the tickets [8af92dfb66] (bad stream expansion) and [f70ce1fead] (multi-stream decoding without readahead limit shenanigans) are fixed. Verified that the tests fail with commit [4eb66645d6], just before the zlib fixes. Tweaked the zlib manpage paragraphs about -limit. Tweaked some code comments. Fixed typos in description of two unrelated zlib tests. check-in2020-06-23 06:37:599215e1ba40added (reworked) patch suggested by aku in [f70ce1fead]: recognize stream is done (and unget the unprocessed data back to input channel) check-in2020-04-16 11:54:34
Updated patch. In addition to signaling the stream end it also records how much of the last read was processed, and how much was not, i.e. read beyond the end of the stream (over'shot). When the transform closes (is popped off) this excess data is stashed back into the underlying channel, to be read either plain, or by the next transform push onto it.
This change makes the `-limit` option superfluous.
We can read as much as we like.
For backward compatibility we should keep accepting it in all releases before v9, just ignore the value, use something automatic (buffer size ?). In v9 we can drop it.
Passes `make test`. Even so, this and previous ticket really need test cases to demo them added to zlib.test. Not today however, need sleep.
Index: generic/tclZlib.c
==================================================================
--- generic/tclZlib.c
+++ generic/tclZlib.c
@@ -126,10 +126,16 @@
Tcl_TimerToken timer; /* Timer used for keeping events fresh. */
Tcl_DString decompressed; /* Buffer for decompression results. */
Tcl_Obj *compDictObj; /* Byte-array object containing compression
* dictionary (not dictObj!) to use if
* necessary. */
+ int done; /* Flag to signal stream end up to transform
+ * input */
+ int over; /* Number of bytes we read to far in the
+ * underlying stream */
+ int skip; /* Bytes processed in the last read before
+ * the overshot */
} ZlibChannelData;
/*
* Value bits for the flags field. Definitions are:
- ASYNC - Whether this is an asynchronous channel. @@ -2946,10 +2952,14 @@ } while (e != Z_STREAM_END); (void) deflateEnd(&cd->outStream); } else { (void) inflateEnd(&cd->inStream); } + + if (cd->over) { + Tcl_Ungets (cd->parent, cd->inBuffer + cd->skip, cd->over, 0); + }
/* * Release all memory. */
@@ -3011,10 +3021,14 @@ gotBytes += copied;
if (toRead == 0) { return gotBytes; } + + if (cd->done) { + return gotBytes; + }
/* * The buffer is exhausted, but the caller wants even more. We now * have to go to the underlying channel, get more bytes and then * transform them for delivery. We may not get what we want (full EOF @@ -3831,10 +3845,15 @@ */
if (((flush == Z_SYNC_FLUSH) && (e == Z_BUF_ERROR)) || (e == Z_STREAM_END) || (e == Z_OK && written == 0)) { + if (e == Z_STREAM_END) { + cd->done = 1; + cd->over = cd->inStream.avail_in; + cd->skip = n - cd->over; + } return TCL_OK; }
/* * Z_BUF_ERROR can be ignored as per http://www.zlib.net/zlib_how.html
Note: Work is done in branch zlib-chan-f70ce1fead
Thanks to sebres.
Number of things: - Folded `done` into the `flags` field. - Fixed possible buffer overflow when readahead limit is lrger than default buffer. - Reorganized processing loop to avoid superfluous operations, and merge similar parts.
There is a new branch zlib-chan-f70ce1fead trying to implement that:
- applied reworked patch suggested by Andreas (and with rewritten main cycle);
- default read ahead limit set to 4K (instead of 1);
- also avoid SF/BO (as replacement for [54605d16b9]);
- small enhancement and fix [8f17e96ef5] with: code simplification, no interim copy/move buffers, no decompression d-string, the inflate occurring in single step (ResultDecompress combines ResultGenerate and ResultCopy); better handling in non-blocking mode (also recognizes or distinguish no data/EAGAIN cases)
I think there is a lot of test-cases at the moment zlib misses to be fully covered.
merged, close