Skip to content

Commit f3bd24a

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: Fix #81659: stream_get_contents() may unnecessarily overallocate
2 parents 297117b + 31749aa commit f3bd24a

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ PHP NEWS
3333
- Standard:
3434
. Fixed bug #81618 (dns_get_record fails on FreeBSD for missing type).
3535
(fsbruva)
36+
. Fixed bug #81659 (stream_get_contents() may unnecessarily overallocate).
37+
(cmb)
3638

3739
18 Nov 2021, PHP 8.0.13
3840

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Bug #81659 (stream_get_contents() may unnecessarily overallocate)
3+
--FILE--
4+
<?php
5+
$stream = fopen(__DIR__ . "/81659.txt", "w+");
6+
7+
for ($i = 0; $i < 1024; $i++) {
8+
fwrite($stream, str_repeat("*", 1024));
9+
}
10+
11+
fseek($stream, 1023 * 1024);
12+
13+
$m0 = memory_get_peak_usage();
14+
var_dump(strlen(stream_get_contents($stream)));
15+
$m1 = memory_get_peak_usage();
16+
var_dump($m1 < $m0 + 512 * 1024);
17+
?>
18+
--CLEAN--
19+
<?php
20+
@unlink(__DIR__ . "/81659.txt");
21+
?>
22+
--EXPECT--
23+
int(1024)
24+
bool(true)

main/streams/streams.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,9 +1495,9 @@ PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, int
14951495
* result may be inaccurate, as the filter may inflate or deflate the
14961496
* number of bytes that we can read. In order to avoid an upsize followed
14971497
* by a downsize of the buffer, overestimate by the step size (which is
1498-
* 2K). */
1498+
* 8K). */
14991499
if (php_stream_stat(src, &ssbuf) == 0 && ssbuf.sb.st_size > 0) {
1500-
max_len = ssbuf.sb.st_size + step;
1500+
max_len = MAX(ssbuf.sb.st_size - src->position, 0) + step;
15011501
} else {
15021502
max_len = step;
15031503
}

0 commit comments

Comments
 (0)