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