Artifacts Associated With Ticket f70ce1fead
Ticket change [adc768c168] (rid 181775) by aku on 2020-04-16 05:45:24:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-wiki"
- comment initialized to:
This is a followup to ticket [8af92dfb668b292a7b21df9ac0418e3bc2e68be]. 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. <pre> 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 } </pre> 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 <pre> 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 </pre> 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. - foundin initialized to: "8.6.10"
- is_private initialized to: "0"
- login: "aku"
- priority initialized to: "5 Medium"
- private_contact initialized to: "194c9ef8edbd9a57ef36f4f18fde1d57ce26e1e5"
- resolution initialized to: "None"
- severity initialized to: "Critical"
- status initialized to: "Open"
- submitter initialized to: "aku"
- subsystem initialized to: "57. zlib"
- title initialized to:
zlib transform issue, multi-stream inflate ignores streams after first
- type initialized to: "Bug"
Ticket change [7fa4bde922] (rid 181777) by aku on 2020-04-16 06:53:41:
- icomment:
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. <pre> 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 </pre> - login: "aku"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [b3a0263bb9] (rid 181820) by aku on 2020-04-16 15:53:33:
- icomment:
Note: Work is done in branch <a href='timeline?r=zlib-chan-f70ce1fead'>zlib-chan-f70ce1fead</a> 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.
- login: "aku"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [27612d27b9] (rid 181848) by sebres on 2020-04-16 19:16:00:
- icomment:
There is a new branch [/timeline?r=zlib-chan-f70ce1fead|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.
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Pending"
- icomment:
Ticket change [b3d61ee2e0] (rid 184209) by sebres on 2020-06-24 09:02:59:
- closedate changed to: "2459024.87707861"
- closer changed to: "sebres"
- icomment: "merged, close"
- login: "sebres"
- mimetype: "text/plain"
- status changed to: "Closed"