Skip to content

Commit 67421a7

Browse files
liudaixiaonikic
liudaixiao
authored andcommitted
Fixed bug #78902
1 parent f720fb1 commit 67421a7

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ PHP NEWS
2929
. Fixed bug #79151 (heap use after free caused by
3030
spl_dllist_it_helper_move_forward). (Nikita)
3131

32+
- Standard:
33+
. Fixed bug #78902 (Memory leak when using stream_filter_append). (liudaixiao)
34+
3235
23 Jan 2020, PHP 7.3.14
3336

3437
- Core

ext/standard/tests/streams/bug78902.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
--TEST--
22
Bug #78902: Memory leak when using stream_filter_append
3-
--XFAIL--
4-
This bug was introduced in PHP 7.3.11 an is still open
53
--INI--
64
memory_limit=512k
75
--FILE--

main/streams/streams.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,15 @@ PHPAPI void _php_stream_fill_read_buffer(php_stream *stream, size_t size)
577577
* stream read buffer */
578578
while (brig_inp->head) {
579579
bucket = brig_inp->head;
580-
/* grow buffer to hold this bucket
581-
* TODO: this can fail for persistent streams */
580+
/* reduce buffer memory consumption if possible, to avoid a realloc */
581+
if (stream->readbuf && stream->readbuflen - stream->writepos < bucket->buflen) {
582+
if (stream->writepos > stream->readpos) {
583+
memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->writepos - stream->readpos);
584+
}
585+
stream->writepos -= stream->readpos;
586+
stream->readpos = 0;
587+
}
588+
/* grow buffer to hold this bucket */
582589
if (stream->readbuflen - stream->writepos < bucket->buflen) {
583590
stream->readbuflen += bucket->buflen;
584591
stream->readbuf = perealloc(stream->readbuf, stream->readbuflen,

0 commit comments

Comments
 (0)