Merge branch 'PHP-8.0' into PHP-8.1

* PHP-8.0:
  Fix #81659: stream_get_contents() may unnecessarily overallocate
This commit is contained in:
Christoph M. Becker 2021-11-29 14:49:55 +01:00
commit b0823438a9
No known key found for this signature in database
GPG key ID: D66C9593118BCCB6
3 changed files with 30 additions and 2 deletions

4
NEWS
View file

@ -9,6 +9,10 @@ PHP NEWS
- PCRE: - PCRE:
. Update bundled PCRE2 to 10.39. (cmb) . 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 25 Nov 2021, PHP 8.1.0
- Core: - Core:

View file

@ -0,0 +1,24 @@
--TEST--
Bug #81659 (stream_get_contents() may unnecessarily overallocate)
--FILE--
<?php
$stream = fopen(__DIR__ . "/81659.txt", "w+");
for ($i = 0; $i < 1024; $i++) {
fwrite($stream, str_repeat("*", 1024));
}
fseek($stream, 1023 * 1024);
$m0 = memory_get_peak_usage();
var_dump(strlen(stream_get_contents($stream)));
$m1 = memory_get_peak_usage();
var_dump($m1 < $m0 + 512 * 1024);
?>
--CLEAN--
<?php
@unlink(__DIR__ . "/81659.txt");
?>
--EXPECT--
int(1024)
bool(true)

View file

@ -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 * 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 * 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 * 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) { 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 { } else {
max_len = step; max_len = step;
} }