Bug #71395 [Opn]: zlib.deflate fails on empty data

From: Date: Thu, 03 Mar 2016 05:01:28 +0000
Subject: Bug #71395 [Opn]: zlib.deflate fails on empty data
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199571@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71395&edit=1 ID: 71395 User updated by: salsi at icosaedro dot it Reported by: salsi at icosaedro dot it -Summary: zlib.compress fails on empty data +Summary: zlib.deflate fails on empty data Status: Open Type: Bug Package: Streams related PHP Version: master-Git-2016-01-17 (Git) Block user comment: N Private report: N New Comment: Run into this same issue again. Maybe a shorter test script might help to illustrate how the stream filter works under PHP 5 and fails under PHP 7.1: <?php // Deflate stream filter with empty data writes an empty file instead of an empty // deflate stream on PHP 7.1. // Works as expected on PHP 5.6.3, same box Slackware 14.1 with zlib v. 1.2.8. // As stated in http://www.zlib.net, zlib v. 1.2.8 should had solved the bug // when gzopen() immediately followed by gzclose() would write an empty file. $fn = "empty.deflated"; $data = ""; $f = fopen($fn, "wb"); stream_filter_append($f, 'zlib.deflate', STREAM_FILTER_WRITE, array('level' => -1, 'window' => -15, 'memory' => 9) ); fwrite($f, $data); fclose($f); echo "gzdeflate(): ", bin2hex(gzdeflate($data)), "\n"; echo "deflate stream: ", bin2hex(file_get_contents($fn)), "\n"; ?> Output of PHP 5.6.3 (cli) (built: Nov 17 2014 20:57:12): -------------------------------------------------------- gzdeflate(): 0300 deflate stream: 0300 Output of PHP 7.1.0-dev (cli) (built: Mar 2 2016 07:50:35) ( NTS ): -------------------------------------------------------------------- gzdeflate(): 0300 deflate stream: Previous Comments: ------------------------------------------------------------------------ [2016-01-17 10:22:05] salsi at icosaedro dot it Description: ------------ The zlib.compress filter writes nothing when the data to write are empty. Instead, the DEFLATE algo (RFC 1950) requires to write at least one last block of data, as the gzdeflate() function correctly does: gzdeflate("") --> "\x03\x00" The result is that the written file cannot be decoded with gzinflate() withour error. In fact gzinflate() correctly recognizes the empty string as invalid DEFLATE format (there must be at least one block of compressed data with the first but set to mark the last block of data). The following test code, perfectly working as expected under PHP 5.6.3, fails as stated above on PHP 7. Test script: --------------- <?php /* * The zlib.compress filter writes nothing if the data to compress are the empty * string. * PHP 7.1.0-dev from git 2016-01-17 */ // Set a safe test environment: error_reporting(-1); // maps errors to ErrorException: function my_error_handler($errno, $message) { throw new ErrorException($message); } set_error_handler("my_error_handler"); /** * @param string $plain */ function test($plain) { echo "Now testing with $plain:\n"; $fn = "test"; $f = fopen($fn, "wb"); stream_filter_append($f, 'zlib.deflate', STREAM_FILTER_WRITE); fwrite($f, $plain); fclose($f); $compressed = file_get_contents($fn); echo "compressed: ", rawurlencode($compressed), "\n"; $plain2 = gzinflate($compressed); echo "decompressed: ", rawurlencode($plain2), "\n"; } echo "gzdeflate(\"\"): ", rawurlencode(gzdeflate("")), "\n"; test("abc"); // ok test(""); // no data written, error on gzinflate() ?> Expected result: ---------------- gzdeflate(""): %03%00 Now testing with abc: compressed: KLJ%06%00 decompressed: abc Now testing with : compressed: %03%00 decompressed: (this actually is the output of PHP 5.6.3) Actual result: -------------- gzdeflate(""): %03%00 Now testing with abc: compressed: KLJ%06%00 decompressed: abc Now testing with : compressed: PHP Fatal error: Uncaught ErrorException: gzinflate(): data error in /home/salsi/src/phplint/bugs/bug-zlib-compress-filter-on-empty-string.php:12 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71395&edit=1

« previous php.bugs (#199571) next »