[php-src] Issue #11242: _php_stream_copy_to_mem: Allow specifying a maximum length without allocating a buffer of that size
| From: | naderman | Date: | Mon, 15 May 2023 09:24:18 +0000 |
| Subject: | [php-src] Issue #11242: _php_stream_copy_to_mem: Allow specifying a maximum length without allocating a buffer of that size | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244452@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11242
Author: naderman
### 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.