Bug #71395 [NEW]: zlib.compress fails on empty data

From: Date: Sun, 17 Jan 2016 10:22:13 +0000
Subject: Bug #71395 [NEW]: zlib.compress fails on empty data
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198729@lists.php.net to get a copy of this message
From: salsi at icosaedro dot it Operating system: PHP version: master-Git-2016-01-17 (Git) Package: Streams related Bug Type: Bug Bug description:zlib.compress fails on empty data 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 bug report at https://bugs.php.net/bug.php?id=71395&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71395&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71395&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71395&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=71395&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=71395&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=71395&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=71395&r=needscript Try newer version: https://bugs.php.net/fix.php?id=71395&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=71395&r=support Expected behavior: https://bugs.php.net/fix.php?id=71395&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=71395&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=71395&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=71395&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71395&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=71395&r=dst IIS Stability: https://bugs.php.net/fix.php?id=71395&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=71395&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=71395&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=71395&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=71395&r=mysqlcfg

« previous php.bugs (#198729) next »