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