Skip to content

Commit c35dc45

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

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

ext/bz2/bz2_filter.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ 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+
pefree(data->strm.next_in, persistent);
444+
pefree(data->strm.next_out, persistent);
445+
pefree(data, persistent);
446+
return NULL;
443447
} else {
444448
blockSize100k = (int) blocks;
445449
}
@@ -450,6 +454,10 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
450454
zend_long work = zval_get_long(tmpzval);
451455
if (work < 0 || work > 250) {
452456
php_error_docref(NULL, E_WARNING, "Invalid parameter given for work factor (" ZEND_LONG_FMT ")", work);
457+
pefree(data->strm.next_in, persistent);
458+
pefree(data->strm.next_out, persistent);
459+
pefree(data, persistent);
460+
return NULL;
453461
} else {
454462
workFactor = (int) work;
455463
}

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)