Procházet zdrojové kódy

fix: convert stream filter warnings to ZipException on entry read

PHP 8.4 made the zlib inflate filter emit an E_WARNING (PHP 8.5 an
E_NOTICE) when fed garbage, e.g. while decrypting with a wrong password.
The warning escaped as a non-ZipException diagnostic and broke
ZipPasswordTest on PHP 8.4. Wrap the filtered read loop in
set_error_handler and throw ZipException uniformly on all versions.
Ne-Lexa před 3 týdny
rodič
revize
a512228521
1 změnil soubory, kde provedl 20 přidání a 0 odebrání
  1. 20 0
      src/IO/ZipReader.php

+ 20 - 0
src/IO/ZipReader.php

@@ -811,6 +811,24 @@ class ZipReader
         $chunkSize = 8192;
 
         try {
+            // PHP 8.4+ emits warnings (and PHP 8.5+ notices) from stream filters,
+            // e.g. "fread(): zlib: data error" when a decryption filter produced
+            // garbage. Convert these diagnostics into ZipException so that every
+            // PHP version reports a uniform failure.
+            set_error_handler(
+                static function (int $errorNumber, string $errorString) use ($entry): bool {
+                    throw new ZipException(
+                        sprintf(
+                            'Error reading the contents of entry "%s". %s',
+                            $entry->getName(),
+                            $errorString
+                        ),
+                        $errorNumber
+                    );
+                },
+                \E_WARNING | \E_USER_WARNING | \E_NOTICE | \E_USER_NOTICE
+            );
+
             if ($skipCheckCrc) {
                 while ($offset < $limit) {
                     $length = min($chunkSize, $limit - $offset);
@@ -844,6 +862,8 @@ class ZipReader
                 }
             }
         } finally {
+            restore_error_handler();
+
             for ($i = \count($filters); $i > 0; $i--) {
                 stream_filter_remove($filters[$i - 1]);
             }