Skip to content

Commit 894e17c

Browse files
committed
Merge branch 'PHP-8.3'
2 parents a470110 + caec2b6 commit 894e17c

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed
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
@@ -189,22 +189,7 @@ php_stream_filter_status_t userfilter_filter(
189189
}
190190

191191
if (buckets_in->head) {
192-
php_stream_bucket *bucket;
193-
194192
php_error_docref(NULL, E_WARNING, "Unprocessed filter buckets remaining on input brigade");
195-
while ((bucket = buckets_in->head)) {
196-
/* Remove unconsumed buckets from the brigade */
197-
php_stream_bucket_unlink(bucket);
198-
php_stream_bucket_delref(bucket);
199-
}
200-
}
201-
if (ret != PSFS_PASS_ON) {
202-
php_stream_bucket *bucket = buckets_out->head;
203-
while (bucket != NULL) {
204-
php_stream_bucket_unlink(bucket);
205-
php_stream_bucket_delref(bucket);
206-
bucket = buckets_out->head;
207-
}
208193
}
209194

210195
/* 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
@@ -636,6 +636,17 @@ PHPAPI zend_result _php_stream_fill_read_buffer(php_stream *stream, size_t size)
636636
/* some fatal error. Theoretically, the stream is borked, so all
637637
* further reads should fail. */
638638
stream->eof = 1;
639+
/* free all data left in brigades */
640+
while ((bucket = brig_inp->head)) {
641+
/* Remove unconsumed buckets from the input brigade */
642+
php_stream_bucket_unlink(bucket);
643+
php_stream_bucket_delref(bucket);
644+
}
645+
while ((bucket = brig_outp->head)) {
646+
/* Remove unconsumed buckets from the output brigade */
647+
php_stream_bucket_unlink(bucket);
648+
php_stream_bucket_delref(bucket);
649+
}
639650
efree(chunk_buf);
640651
retval = FAILURE;
641652
goto out_is_eof;

0 commit comments

Comments
 (0)