From dbc3e640a2807e90bad8ebaef6f4ebb06178a18c Mon Sep 17 00:00:00 2001 From: aku Date: Tue, 23 Jun 2020 06:37:59 +0000 Subject: Added 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. --- doc/zlib.n | 18 ++++++++++----- generic/tclZlib.c | 5 +++-- tests/assets/zlib.bin | Bin 0 -> 591 bytes tests/zlib.test | 60 ++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 tests/assets/zlib.bin diff --git a/doc/zlib.n b/doc/zlib.n index 994a063..3714fc1 100644 --- a/doc/zlib.n +++ b/doc/zlib.n @@ -193,12 +193,18 @@ How hard to compress the data. Must be an integer from 0 (uncompressed) to 9 .TP \fB\-limit\fI readaheadLimit\fR . -The maximum number of bytes ahead to read when decompressing. This can be set -to 1 to find the border for multi-stream inflate or mixed data, to ensure it -would handle correctly e. g. some operations on following streams after first -caused to due time. -Otherwise Tcl would try to automatically return previously read extra data -to input stream back. +The maximum number of bytes ahead to read when decompressing. +.RS +.PP +This option has become \fBirrelevant\fR. It was originally introduced +to prevent Tcl from reading beyond the end of a compressed stream in +multi-stream channels to ensure that the data after was left alone for +further reading, at the cost of speed. +.PP +Tcl now automatically returns any bytes it has read beyond the end of +a compressed stream back to the channel, making them appear as unread +to further readers. +.RE .PP Both compressing and decompressing channel transformations add extra configuration options that may be accessed through \fBchan configure\fR. The diff --git a/generic/tclZlib.c b/generic/tclZlib.c index 68bdf92..42d7156 100644 --- a/generic/tclZlib.c +++ b/generic/tclZlib.c @@ -2948,8 +2948,9 @@ ZlibTransformClose( (void) deflateEnd(&cd->outStream); } else { /* - * If we have rest of read input (overshot by Z_STREAM_END or on possible error), - * unget this part of buffer back to the parent channel. + * If we have unused bytes from the read input (overshot by + * Z_STREAM_END or on possible error), unget them back to the parent + * channel, so that they appear as not being read yet. */ if (cd->inStream.avail_in) { Tcl_Ungets (cd->parent, (char *)cd->inStream.next_in, cd->inStream.avail_in, 0); diff --git a/tests/assets/zlib.bin b/tests/assets/zlib.bin new file mode 100644 index 0000000..dadb639 Binary files /dev/null and b/tests/assets/zlib.bin differ diff --git a/tests/zlib.test b/tests/zlib.test index c2f7825..e7ffcfe 100644 --- a/tests/zlib.test +++ b/tests/zlib.test @@ -920,7 +920,7 @@ test zlib-10.2 "bug #2818131 (mismatch gets)" -constraints { rename zlibRead {} } -result {error {invalid block type}} -test zlib-11.1 "Bug #3390073: mis-appled gzip filtering" -setup { +test zlib-11.1 "Bug #3390073: mis-applied gzip filtering" -setup { set file [makeFile {} test.input] } -constraints zlib -body { set f [open $file wb] @@ -934,7 +934,7 @@ test zlib-11.1 "Bug #3390073: mis-appled gzip filtering" -setup { } -cleanup { removeFile $file } -result {1000 0} -test zlib-11.2 "Bug #3390073: mis-appled gzip filtering" -setup { +test zlib-11.2 "Bug #3390073: mis-applied gzip filtering" -setup { set file [makeFile {} test.input] } -constraints zlib -body { set f [open $file wb] @@ -1005,6 +1005,62 @@ test zlib-12.2 {Patrick Dunnigan's issue} -constraints zlib -setup { removeFile $filesrc removeFile $filedst } -result 56 + +test zlib-13.1 {Ticket 8af92dfb66 - zlib stream mis-expansion} -constraints zlib -setup { + set pathin [file join $::tcltest::testsDirectory assets zlib.bin] + set chanin [open $pathin rb] + set pathout [file join [pwd] zlib.deflated] + set chanout [open $pathout wb] + zlib push inflate $chanin + fcopy $chanin $chanout + close $chanin + close $chanout +} -body { + file size $pathout +} -cleanup { + file delete $pathout + unset chanin pathin chanout pathout +} -result 458752 + +test zlib-13.2 {Ticket f70ce1fead - zlib multi-stream expansion} -constraints zlib -setup { + # Start from the basic asset + set pathin [file join $::tcltest::testsDirectory assets zlib.bin] + set chanin [open $pathin rb] + # Create a multi-stream by copying the asset twice into it. + set pathout [file join [pwd] zlib.multi] + set chanout [open $pathout wb] + fcopy $chanin $chanout + seek $chanin 0 start + fcopy $chanin $chanout + close $chanin + close $chanout + # The multi-stream file shall be our input + set pathin $pathout + set chanin [open $pathin rb] + # And our destinations + set pathout1 [file join [pwd] zlib.1] + set pathout2 [file join [pwd] zlib.2] +} -body { + # Decode first stream + set chanout [open $pathout1 wb] + zlib push inflate $chanin + fcopy $chanin $chanout + chan pop $chanin + close $chanout + # Decode second stream + set chanout [open $pathout2 wb] + zlib push inflate $chanin + fcopy $chanin $chanout + chan pop $chanin + close $chanout + # + list [file size $pathout1] [file size $pathout2] +} -cleanup { + close $chanin + file delete $pathout $pathout1 $pathout2 + unset chanin pathin chanout pathout pathout1 pathout2 +} -result {458752 458752} + ::tcltest::cleanupTests return -- cgit v0.12