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