Skip to content

Commit 4893048

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

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
@@ -9,6 +9,10 @@ PHP NEWS
99
initialized lazy proxies). (iliaal)
1010
. Fixed bug GH-21605 (Missing addref for Countable::count()). (ilutov)
1111

12+
- Bz2:
13+
. Fixed bug GH-19685 (Segfault when bzip2 filter has invalid parameters).
14+
(alexandre-daubois)
15+
1216
- Curl:
1317
. Add support for brotli and zstd on Windows. (Shivam Mathur)
1418

ext/bz2/bz2_filter.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
371371
zend_long blocks = zval_get_long(tmpzval);
372372
if (blocks < 1 || blocks > 9) {
373373
php_error_docref(NULL, E_WARNING, "Invalid parameter given for number of blocks to allocate (" ZEND_LONG_FMT ")", blocks);
374+
pefree(data->strm.next_in, persistent);
375+
pefree(data->strm.next_out, persistent);
376+
pefree(data, persistent);
377+
return NULL;
374378
} else {
375379
blockSize100k = (int) blocks;
376380
}
@@ -381,6 +385,10 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
381385
zend_long work = zval_get_long(tmpzval);
382386
if (work < 0 || work > 250) {
383387
php_error_docref(NULL, E_WARNING, "Invalid parameter given for work factor (" ZEND_LONG_FMT ")", work);
388+
pefree(data->strm.next_in, persistent);
389+
pefree(data->strm.next_out, persistent);
390+
pefree(data, persistent);
391+
return NULL;
384392
} else {
385393
workFactor = (int) work;
386394
}

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)