bug71417.phpt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. --TEST--
  2. Bug #71417: fread() does not report zlib.inflate errors
  3. --EXTENSIONS--
  4. zlib
  5. --FILE--
  6. <?php
  7. function test($case) {
  8. $plain = "The quick brown fox jumps over the lazy dog.";
  9. $fn = "bug71417.gz";
  10. $compressed = (string) gzencode($plain);
  11. if ($case == 1) {
  12. // 1. Set a random byte in the middle of the compressed data.
  13. // $ php test-zlib-inflate.php
  14. // --> read: string(0) ""
  15. // --> read: string(44) "The quick brown fox jumps over the lazx8dog."
  16. // $ gzip test-zlib-inflate.gz
  17. // gzip: test-zlib-inflate.gz: invalid compressed data--crc error
  18. $compressed[strlen($compressed) - 15] = 'X';
  19. } else if ($case == 2) {
  20. // 2. Truncate the compressed data.
  21. // $ php test-zlib-inflate.php
  22. // --> read: string(32) "The quick brown fox jumps over t"
  23. // $ gzip test-zlib-inflate.gz
  24. // gzip: test-zlib-inflate.gz: unexpected end of file
  25. $compressed = substr($compressed, 0, strlen($compressed) - 20);
  26. } else if ($case == 3) {
  27. // 3. Corrupted final CRC.
  28. // $ php test-zlib-inflate.php
  29. // --> read: string(0) ""
  30. // --> read: string(44) "The quick brown fox jumps over the lazy dog."
  31. // $ gzip test-zlib-inflate.gz
  32. // gzip: test-zlib-inflate.gz: invalid compressed data--crc error
  33. $compressed[strlen($compressed)-5] = 'X';
  34. } else if ($case == 4) {
  35. // 4. Corrupted final length.
  36. // $ php test-zlib-inflate.phpread: string(0) ""
  37. // read: string(44) "The quick brown fox jumps over the lazy dog."
  38. // $ gunzip test-zlib-inflate.gz
  39. // gzip: test-zlib-inflate.gz: invalid compressed data--length error
  40. $compressed[strlen($compressed)-2] = 'X';
  41. }
  42. // The gzdecode() function applied to the corrupted compressed data always
  43. // detects the error:
  44. // --> gzdecode(): PHP Fatal error: Uncaught ErrorException: gzdecode(): data error in ...
  45. echo "gzdecode(): ", rawurldecode(gzdecode($compressed)), "\n";
  46. file_put_contents($fn, $compressed);
  47. $r = fopen($fn, "r");
  48. stream_filter_append($r, 'zlib.inflate', STREAM_FILTER_READ, array('window' => 15+16));
  49. while (!feof($r)) {
  50. $s = fread($r, 100);
  51. echo "read: "; var_dump($s);
  52. }
  53. fclose($r);
  54. unlink($fn);
  55. }
  56. test(1);
  57. test(2);
  58. test(3);
  59. test(4);
  60. ?>
  61. --EXPECTF--
  62. gzdecode():
  63. Warning: gzdecode(): data error in %s on line %d
  64. Notice: fread(): zlib: data error in %s on line %d
  65. read: bool(false)
  66. gzdecode():
  67. Warning: gzdecode(): data error in %s on line %d
  68. read: string(32) "The quick brown fox jumps over t"
  69. gzdecode():
  70. Warning: gzdecode(): data error in %s on line %d
  71. Notice: fread(): zlib: data error in %s on line %d
  72. read: bool(false)
  73. gzdecode():
  74. Warning: gzdecode(): data error in %s on line %d
  75. Notice: fread(): zlib: data error in %s on line %d
  76. read: bool(false)