Similar to what fread() does, truncate the stream_get_contents()
result if the original buffer was way too large.
- Standard:
. Fixed bug #69100 (Bus error from stream_copy_to_stream (file -> SSL stream)
with invalid length). (Nikita)
+ . Fixed bug #78326 (improper memory deallocation on stream_get_contents()
+ with fixed length buffer). (Albert Casademont)
01 Aug 2019, PHP 7.2.21
--- /dev/null
+--TEST--
+memory allocation on stream_get_contents()
+--INI--
+memory_limit=32M
+--FILE--
+<?php
+$f = tmpfile();
+fwrite($f, '.');
+
+$chunks = array();
+for ($i = 0; $i < 1000; ++$i) {
+ rewind($f);
+ $chunks[] = stream_get_contents($f, 1000000);
+}
+var_dump(count($chunks));
+?>
+--EXPECT--
+int(1000)
--- /dev/null
+--TEST--
+proper string length on stream_get_contents()
+--FILE--
+<?php
+$f = fopen('php://memory', 'rw');
+fwrite($f, str_repeat('X', 1000));
+fseek($f, 0);
+var_dump(strlen(stream_get_contents($f, 1024)));
+--EXPECT--
+int(1000)
ptr += ret;
}
if (len) {
- *ptr = '\0';
ZSTR_LEN(result) = len;
+ ZSTR_VAL(result)[len] = '\0';
+
+ /* Only truncate if the savings are large enough */
+ if (len < maxlen / 2) {
+ result = zend_string_truncate(result, len, persistent);
+ }
} else {
zend_string_free(result);
result = NULL;