Skip to content

Commit c087398

Browse files
committed
Fix GH-13264: Part 1 - Memory leak on filter failure
Closes GH-13790
1 parent cd6a581 commit c087398

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ PHP NEWS
66
. Fixed bug GH-13563 (Setting bool values via env in FPM config fails).
77
(Jakub Zelenka)
88

9+
- Streams:
10+
. Fixed bug GH-13264 (Part 1 - Memory leak on stream filter failure).
11+
(Jakub Zelenka)
12+
913
11 Apr 2024, PHP 8.2.18
1014

1115
- Core:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
GH-81475: Memory leak during stream filter failure
3+
--SKIPIF--
4+
<?php require 'filter_errors.inc'; filter_errors_skipif('zlib.inflate'); ?>
5+
--FILE--
6+
<?php
7+
// Prepare a big enough input so that it is not entirely buffered
8+
$stream = fopen('php://memory', 'r+');
9+
$content = '';
10+
for ($i = 0; $i < 10000; $i++) {
11+
$content .= "Hello $i\n";
12+
}
13+
fwrite($stream, gzcompress($content));
14+
15+
// Mess up the checksum
16+
fseek($stream, -1, SEEK_CUR);
17+
fwrite($stream, '1');
18+
19+
// Rewind and add the zlib filter
20+
rewind($stream);
21+
stream_filter_append($stream, 'zlib.inflate', STREAM_FILTER_READ, ['window' => 15]);
22+
23+
// Read the filtered stream line by line.
24+
while (($line = fgets($stream)) !== false) {
25+
$error = error_get_last();
26+
if ($error !== null) {
27+
// An error is thrown but fgets didn't return false
28+
var_dump(error_get_last());
29+
var_dump($line);
30+
}
31+
}
32+
33+
fclose($stream);
34+
?>
35+
--EXPECTF--
36+
37+
Notice: fgets(): zlib: data error in %s on line %d
38+
array(4) {
39+
["type"]=>
40+
int(8)
41+
["message"]=>
42+
string(25) "fgets(): zlib: data error"
43+
["file"]=>
44+
string(%d) "%s"
45+
["line"]=>
46+
int(%d)
47+
}
48+
string(7) "Hello 6"
49+

ext/standard/user_filters.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,7 @@ php_stream_filter_status_t userfilter_filter(
196196
}
197197

198198
if (buckets_in->head) {
199-
php_stream_bucket *bucket;
200-
201199
php_error_docref(NULL, E_WARNING, "Unprocessed filter buckets remaining on input brigade");
202-
while ((bucket = buckets_in->head)) {
203-
/* Remove unconsumed buckets from the brigade */
204-
php_stream_bucket_unlink(bucket);
205-
php_stream_bucket_delref(bucket);
206-
}
207-
}
208-
if (ret != PSFS_PASS_ON) {
209-
php_stream_bucket *bucket = buckets_out->head;
210-
while (bucket != NULL) {
211-
php_stream_bucket_unlink(bucket);
212-
php_stream_bucket_delref(bucket);
213-
bucket = buckets_out->head;
214-
}
215200
}
216201

217202
/* filter resources are cleaned up by the stream destructor,

main/streams/streams.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,17 @@ PHPAPI zend_result _php_stream_fill_read_buffer(php_stream *stream, size_t size)
632632
/* some fatal error. Theoretically, the stream is borked, so all
633633
* further reads should fail. */
634634
stream->eof = 1;
635+
/* free all data left in brigades */
636+
while ((bucket = brig_inp->head)) {
637+
/* Remove unconsumed buckets from the input brigade */
638+
php_stream_bucket_unlink(bucket);
639+
php_stream_bucket_delref(bucket);
640+
}
641+
while ((bucket = brig_outp->head)) {
642+
/* Remove unconsumed buckets from the output brigade */
643+
php_stream_bucket_unlink(bucket);
644+
php_stream_bucket_delref(bucket);
645+
}
635646
efree(chunk_buf);
636647
return FAILURE;
637648
}

0 commit comments

Comments
 (0)