Bug #81659 [NEW]: stream_get_contents() loading entire file into memory

From: Date: Fri, 26 Nov 2021 04:17:25 +0000
Subject: Bug #81659 [NEW]: stream_get_contents() loading entire file into memory
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237982@lists.php.net to get a copy of this message
From:             terence-marks at zencontrol dot com
Operating system: Debian Buster
PHP version:      8.0.13
Package:          Streams related
Bug Type:         Bug
Bug description:stream_get_contents() loading entire file into memory

Description:
------------
Passing length of -1 to stream_get_contents() with the file position
indicator of resource near the end of file still loads the entire file
into memory. Passing a fixed length to stream_get_contents() does not
produce the same behaviour.

Note: when running test script set memory_limit=64M

Test script:
---------------
<?php

$resource = fopen('/tmp/file1', 'a+');

for ($i = 0; $i < 64; ++$i)
{
    $data = random_bytes(1024 * 1024);
    $position = ftell($resource);
    fwrite($resource, $data);
    fseek($resource, $position);
    stream_get_contents($resource, strlen($data));
}

$resource = fopen('/tmp/file2', 'a+');

for ($i = 0; $i < 64; ++$i)
{
    $data = random_bytes(1024 * 1024);
    $position = ftell($resource);
    fwrite($resource, $data);
    fseek($resource, $position);
    stream_get_contents($resource, -1);
}


Actual result:
--------------
PHP Fatal error:  Allowed memory size of 67108864 bytes exhausted (tried
to allocate 62922784 bytes) in script.php on line 22


-- 
Edit bug report at https://bugs.php.net/bug.php?id=81659&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=81659&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=81659&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=81659&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=81659&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=81659&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=81659&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=81659&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=81659&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=81659&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=81659&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81659&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=81659&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=81659&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=81659&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=81659&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=81659&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=81659&r=mysqlcfg


Thread (4 messages)

« previous php.bugs (#237982) next »