Skip to content

Commit db58e31

Browse files
Fix GH-19685: Segfault when bzip2 filter has invalid parameters
1 parent b6bee2c commit db58e31

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ PHP NEWS
2020
- BCMath:
2121
. Added NUL-byte validation to BCMath functions. (jorgsowa)
2222

23+
- Bz2:
24+
. Fixed bug GH-19685 (Segfault when bzip2 filter has invalid parameters).
25+
(alexandre-daubois)
26+
2327
- Date:
2428
. Update timelib to 2022.16. (Derick)
2529

ext/bz2/bz2_filter.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
442442
zend_long blocks = zval_get_long(tmpzval);
443443
if (blocks < 1 || blocks > 9) {
444444
php_error_docref(NULL, E_WARNING, "Invalid parameter given for number of blocks to allocate (" ZEND_LONG_FMT ")", blocks);
445+
pefree(data->strm.next_in, persistent);
446+
pefree(data->strm.next_out, persistent);
447+
pefree(data, persistent);
448+
return NULL;
445449
} else {
446450
blockSize100k = (int) blocks;
447451
}
@@ -452,6 +456,10 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
452456
zend_long work = zval_get_long(tmpzval);
453457
if (work < 0 || work > 250) {
454458
php_error_docref(NULL, E_WARNING, "Invalid parameter given for work factor (" ZEND_LONG_FMT ")", work);
459+
pefree(data->strm.next_in, persistent);
460+
pefree(data->strm.next_out, persistent);
461+
pefree(data, persistent);
462+
return NULL;
455463
} else {
456464
workFactor = (int) work;
457465
}

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)