Description
Description
Currently, if you specify a maxlen for _php_stream_copy_to_mem other than PHP_STREAM_COPY_ALL, the function will allocate a buffer of that size, see https://github.com/php/php-src/blob/master/main/streams/streams.c#L1480
As a consequence there is no way to limit the length of what you want to copy without allocating a buffer that size. So for example, if you want to limit size of a file you're downloading to 1GB, but typical responses you expect are only a few KB, you still allocate the entire GB of memory each time.
Instead I think there should be a way to pass a maxlen and still get the dynamically growing buffer behavior using chunks of PHP_STREAM_COPY_ALL. Userland functions impacted are for example stream_get_contents
or file_get_contents
.