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