Skip to content

Commit b418fe2

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: Fix #75776: Flushing streams with compression filter is broken
2 parents 1d2bbce + 963e50c commit b418fe2

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2021, PHP 8.0.4
44

5+
- Core:
6+
. Fixed bug #75776 (Flushing streams with compression filter is broken). (cmb)
7+
58
- Intl:
69
. Fixed bug #80763 (msgfmt_format() does not accept DateTime references).
710
(cmb)

ext/bz2/bz2_filter.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef struct _php_bz2_filter_data {
3939
enum strm_status status; /* Decompress option */
4040
unsigned int small_footprint : 1; /* Decompress option */
4141
unsigned int expect_concatenated : 1; /* Decompress option */
42+
unsigned int is_flushed : 1; /* only for compression */
4243

4344
int persistent;
4445
} php_bz2_filter_data;
@@ -227,14 +228,18 @@ static php_stream_filter_status_t php_bz2_compress_filter(
227228
bucket = php_stream_bucket_make_writeable(buckets_in->head);
228229

229230
while (bin < bucket->buflen) {
231+
int flush_mode;
232+
230233
desired = bucket->buflen - bin;
231234
if (desired > data->inbuf_len) {
232235
desired = data->inbuf_len;
233236
}
234237
memcpy(data->strm.next_in, bucket->buf + bin, desired);
235238
data->strm.avail_in = desired;
236239

237-
status = BZ2_bzCompress(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? BZ_FINISH : (flags & PSFS_FLAG_FLUSH_INC ? BZ_FLUSH : BZ_RUN));
240+
flush_mode = flags & PSFS_FLAG_FLUSH_CLOSE ? BZ_FINISH : (flags & PSFS_FLAG_FLUSH_INC ? BZ_FLUSH : BZ_RUN);
241+
data->is_flushed = flush_mode != BZ_RUN;
242+
status = BZ2_bzCompress(&(data->strm), flush_mode);
238243
if (status != BZ_RUN_OK && status != BZ_FLUSH_OK && status != BZ_FINISH_OK) {
239244
/* Something bad happened */
240245
php_stream_bucket_delref(bucket);
@@ -260,11 +265,12 @@ static php_stream_filter_status_t php_bz2_compress_filter(
260265
php_stream_bucket_delref(bucket);
261266
}
262267

263-
if (flags & PSFS_FLAG_FLUSH_CLOSE) {
268+
if (flags & PSFS_FLAG_FLUSH_CLOSE || ((flags & PSFS_FLAG_FLUSH_INC) && !data->is_flushed)) {
264269
/* Spit it out! */
265270
status = BZ_FINISH_OK;
266271
while (status == BZ_FINISH_OK) {
267-
status = BZ2_bzCompress(&(data->strm), BZ_FINISH);
272+
status = BZ2_bzCompress(&(data->strm), (flags & PSFS_FLAG_FLUSH_CLOSE ? BZ_FINISH : BZ_FLUSH));
273+
data->is_flushed = 1;
268274
if (data->strm.avail_out < data->outbuf_len) {
269275
size_t bucketlen = data->outbuf_len - data->strm.avail_out;
270276

@@ -380,6 +386,7 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
380386
}
381387

382388
status = BZ2_bzCompressInit(&(data->strm), blockSize100k, 0, workFactor);
389+
data->is_flushed = 1;
383390
fops = &php_bz2_compress_ops;
384391
} else {
385392
status = BZ_DATA_ERROR;

ext/bz2/tests/bug75776.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Bug #75776 (Flushing streams with compression filter is broken)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('bz2')) die('skip bz2 extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$text = str_repeat('0123456789abcdef', 1000);
10+
11+
$temp = fopen('php://temp', 'r+');
12+
stream_filter_append($temp, 'bzip2.compress', STREAM_FILTER_WRITE);
13+
fwrite($temp, $text);
14+
15+
rewind($temp);
16+
17+
var_dump(bin2hex(stream_get_contents($temp)));
18+
var_dump(ftell($temp));
19+
20+
fclose($temp);
21+
?>
22+
--EXPECT--
23+
string(144) "425a68343141592653599fe7bbbf0001f389007fe03f002000902980026826aa80003ea9061520c6a41954833a9069520d6a41b54837a9071520e6a41d5483ba9079520f6a41f548"
24+
int(72)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Bug #75776 (Flushing streams with compression filter is broken)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('zlib')) die('skip zlib extension not available');
6+
if (!extension_loaded('bz2')) die('skip bz2 extension not available');
7+
?>
8+
--FILE--
9+
<?php
10+
$compression = [
11+
'gz' => ['zlib.deflate', 'gzinflate'],
12+
'bz2' => ['bzip2.compress', 'bzdecompress']
13+
];
14+
foreach ($compression as $ext => [$filter, $function]) {
15+
$stream = fopen(__DIR__ . "/75776.$ext", 'w');
16+
stream_filter_append($stream, $filter);
17+
fwrite($stream,"sdfgdfg");
18+
fflush($stream);
19+
fclose($stream);
20+
21+
$compressed = file_get_contents(__DIR__ . "/75776.$ext");
22+
var_dump($function($compressed));
23+
}
24+
?>
25+
--EXPECT--
26+
string(7) "sdfgdfg"
27+
string(7) "sdfgdfg"
28+
--CLEAN--
29+
<?php
30+
@unlink(__DIR__ . "/75776.gz");
31+
@unlink(__DIR__ . "/75776.bz2");
32+
?>

main/streams/streams.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remov
443443
(close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0);
444444
#endif
445445

446-
if (stream->flags & PHP_STREAM_FLAG_WAS_WRITTEN) {
446+
if (stream->flags & PHP_STREAM_FLAG_WAS_WRITTEN || stream->writefilters.head) {
447447
/* make sure everything is saved */
448448
_php_stream_flush(stream, 1);
449449
}

0 commit comments

Comments
 (0)