Skip to content

Commit fa451f8

Browse files
Fix GH-19685: Reject out-of-range blocks/work values in bzip2.compress filter
1 parent 4592d1c commit fa451f8

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

ext/bz2/bz2_filter.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
440440
zend_long blocks = zval_get_long(tmpzval);
441441
if (blocks < 1 || blocks > 9) {
442442
php_error_docref(NULL, E_WARNING, "Invalid parameter given for number of blocks to allocate (" ZEND_LONG_FMT ")", blocks);
443+
goto cleanup;
443444
} else {
444445
blockSize100k = (int) blocks;
445446
}
@@ -450,6 +451,7 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
450451
zend_long work = zval_get_long(tmpzval);
451452
if (work < 0 || work > 250) {
452453
php_error_docref(NULL, E_WARNING, "Invalid parameter given for work factor (" ZEND_LONG_FMT ")", work);
454+
goto cleanup;
453455
} else {
454456
workFactor = (int) work;
455457
}
@@ -470,13 +472,16 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
470472

471473
if (status != BZ_OK) {
472474
/* Unspecified (probably strm) error, let stream-filter error do its own whining */
473-
pefree(data->strm.next_in, persistent);
474-
pefree(data->strm.next_out, persistent);
475-
pefree(data, persistent);
476-
return NULL;
475+
goto cleanup;
477476
}
478477

479478
return php_stream_filter_alloc(fops, data, persistent, PSFS_SEEKABLE_START);
479+
480+
cleanup:
481+
pefree(data->strm.next_in, persistent);
482+
pefree(data->strm.next_out, persistent);
483+
pefree(data, persistent);
484+
return NULL;
480485
}
481486

482487
const php_stream_filter_factory php_bz2_filter_factory = {

ext/bz2/tests/bug72447.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ unlink('testfile');
1717
?>
1818
--EXPECTF--
1919
Warning: stream_filter_append(): Invalid parameter given for number of blocks to allocate (0) in %s%ebug72447.php on line %d
20+
21+
Warning: stream_filter_append(): Unable to create or locate filter "bzip2.compress" in %s%ebug72447.php on line %d
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
GH-19685: bzip2.compress filter with invalid parameters should fail gracefully
3+
--EXTENSIONS--
4+
bz2
5+
--FILE--
6+
<?php
7+
$stream = fopen('php://memory', 'w+');
8+
9+
// too low
10+
$filter = stream_filter_append($stream, 'bzip2.compress', STREAM_FILTER_WRITE, array('blocks' => 0));
11+
var_dump($filter);
12+
13+
// too high
14+
$filter = stream_filter_append($stream, 'bzip2.compress', STREAM_FILTER_WRITE, array('blocks' => 10));
15+
var_dump($filter);
16+
17+
// too low work
18+
$filter = stream_filter_append($stream, 'bzip2.compress', STREAM_FILTER_WRITE, array('work' => -1));
19+
var_dump($filter);
20+
21+
// too high work
22+
$filter = stream_filter_append($stream, 'bzip2.compress', STREAM_FILTER_WRITE, array('work' => 251));
23+
var_dump($filter);
24+
25+
fclose($stream);
26+
?>
27+
--EXPECTF--
28+
Warning: stream_filter_append(): Invalid parameter given for number of blocks to allocate (0) in %s on line %d
29+
30+
Warning: stream_filter_append(): Unable to create or locate filter "bzip2.compress" in %s on line %d
31+
bool(false)
32+
33+
Warning: stream_filter_append(): Invalid parameter given for number of blocks to allocate (10) in %s on line %d
34+
35+
Warning: stream_filter_append(): Unable to create or locate filter "bzip2.compress" in %s on line %d
36+
bool(false)
37+
38+
Warning: stream_filter_append(): Invalid parameter given for work factor (-1) in %s on line %d
39+
40+
Warning: stream_filter_append(): Unable to create or locate filter "bzip2.compress" in %s on line %d
41+
bool(false)
42+
43+
Warning: stream_filter_append(): Invalid parameter given for work factor (251) in %s on line %d
44+
45+
Warning: stream_filter_append(): Unable to create or locate filter "bzip2.compress" in %s on line %d
46+
bool(false)

0 commit comments

Comments
 (0)