xref: /PHP-8.2/ext/bz2/tests/bug71263.phpt (revision 56f90492)
1--TEST--
2Bug #71263: fread() does not report bzip2.decompress errors
3--EXTENSIONS--
4bz2
5--FILE--
6<?php
7
8function 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
43test(1);
44test(2);
45test(3);
46?>
47--EXPECTF--
48Compressed len = 81
49
50Notice: fread(): bzip2 decompression failed in %s on line %d
51read: bool(false)
52Compressed len = 81
53read: string(0) ""
54Compressed len = 81
55
56Notice: fread(): bzip2 decompression failed in %s on line %d
57read: bool(false)
58