bug71263.phpt 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --TEST--
  2. Bug #71263: fread() does not report bzip2.decompress errors
  3. --EXTENSIONS--
  4. bz2
  5. --FILE--
  6. <?php
  7. function test($case) {
  8. $plain = "The quick brown fox jumps over the lazy dog.";
  9. $fn = "bug71263.bz2";
  10. $compressed = (string) bzcompress($plain);
  11. echo "Compressed len = ", strlen($compressed), "\n";
  12. if ($case == 1) {
  13. // Set a random byte in the middle of the compressed data
  14. // --> php_bz2_decompress_filter() detects fatal error
  15. // --> fread() displays empty string then garbage, no errors detected:
  16. $compressed[strlen($compressed) - 15] = 'X';
  17. } else if ($case == 2) {
  18. // Truncate the compressed data
  19. // --> php_bz2_decompress_filter() does not detect errors,
  20. // --> fread() displays the empty string:
  21. $compressed = substr($compressed, 0, strlen($compressed) - 20);
  22. } else {
  23. // Corrupted final CRC
  24. // --> php_bz2_decompress_filter() detects fatal error
  25. // --> fread() displays an empty string, then the correct plain text, no error detected:
  26. $compressed[strlen($compressed)-2] = 'X';
  27. }
  28. file_put_contents($fn, $compressed);
  29. $r = fopen($fn, "r");
  30. stream_filter_append($r, 'bzip2.decompress', STREAM_FILTER_READ);
  31. while (!feof($r)) {
  32. $s = fread($r, 100);
  33. echo "read: "; var_dump($s);
  34. }
  35. fclose($r);
  36. unlink($fn);
  37. }
  38. test(1);
  39. test(2);
  40. test(3);
  41. ?>
  42. --EXPECTF--
  43. Compressed len = 81
  44. Notice: fread(): bzip2 decompression failed in %s on line %d
  45. read: bool(false)
  46. Compressed len = 81
  47. read: string(0) ""
  48. Compressed len = 81
  49. Notice: fread(): bzip2 decompression failed in %s on line %d
  50. read: bool(false)