diff --git a/NEWS b/NEWS index 063fca4ccd9..b58f90b45e4 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,10 @@ PHP NEWS - PCRE: . Update bundled PCRE2 to 10.39. (cmb) +- Standard: + . Fixed bug #81659 (stream_get_contents() may unnecessarily overallocate). + (cmb) + 25 Nov 2021, PHP 8.1.0 - Core: diff --git a/ext/standard/tests/streams/bug81659.phpt b/ext/standard/tests/streams/bug81659.phpt new file mode 100644 index 00000000000..62e0cbad181 --- /dev/null +++ b/ext/standard/tests/streams/bug81659.phpt @@ -0,0 +1,24 @@ +--TEST-- +Bug #81659 (stream_get_contents() may unnecessarily overallocate) +--FILE-- + +--CLEAN-- + +--EXPECT-- +int(1024) +bool(true) diff --git a/main/streams/streams.c b/main/streams/streams.c index af15ea6710d..1f73ee65871 100644 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -1504,9 +1504,9 @@ PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, int * result may be inaccurate, as the filter may inflate or deflate the * number of bytes that we can read. In order to avoid an upsize followed * by a downsize of the buffer, overestimate by the step size (which is - * 2K). */ + * 8K). */ if (php_stream_stat(src, &ssbuf) == 0 && ssbuf.sb.st_size > 0) { - max_len = ssbuf.sb.st_size + step; + max_len = MAX(ssbuf.sb.st_size - src->position, 0) + step; } else { max_len = step; }