Skip to content

Commit 44fe025

Browse files
committed
Merge branch 'PHP-7.2' into PHP-7.3
2 parents bbed556 + dc7aa22 commit 44fe025

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ PHP NEWS
2424
- Standard:
2525
. Fixed bug #69100 (Bus error from stream_copy_to_stream (file -> SSL stream)
2626
with invalid length). (Nikita)
27+
. Fixed bug #78326 (improper memory deallocation on stream_get_contents()
28+
with fixed length buffer). (Albert Casademont)
2729

2830
01 Aug 2019, PHP 7.3.8
2931

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
memory allocation on stream_get_contents()
3+
--INI--
4+
memory_limit=32M
5+
--FILE--
6+
<?php
7+
$f = tmpfile();
8+
fwrite($f, '.');
9+
10+
$chunks = array();
11+
for ($i = 0; $i < 1000; ++$i) {
12+
rewind($f);
13+
$chunks[] = stream_get_contents($f, 1000000);
14+
}
15+
var_dump(count($chunks));
16+
?>
17+
--EXPECT--
18+
int(1000)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
proper string length on stream_get_contents()
3+
--FILE--
4+
<?php
5+
$f = fopen('php://memory', 'rw');
6+
fwrite($f, str_repeat('X', 1000));
7+
fseek($f, 0);
8+
var_dump(strlen(stream_get_contents($f, 1024)));
9+
--EXPECT--
10+
int(1000)

main/streams/streams.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,8 +1413,13 @@ PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, int
14131413
ptr += ret;
14141414
}
14151415
if (len) {
1416-
*ptr = '\0';
14171416
ZSTR_LEN(result) = len;
1417+
ZSTR_VAL(result)[len] = '\0';
1418+
1419+
/* Only truncate if the savings are large enough */
1420+
if (len < maxlen / 2) {
1421+
result = zend_string_truncate(result, len, persistent);
1422+
}
14181423
} else {
14191424
zend_string_free(result);
14201425
result = NULL;

0 commit comments

Comments
 (0)