php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67374 Integer Overflow in Memory Usage Functions
Submitted: 2014-06-03 14:42 UTC Modified: 2017-03-16 20:24 UTC
Votes:6
Avg. Score:4.2 ± 0.7
Reproduced:5 of 6 (83.3%)
Same Version:3 (60.0%)
Same OS:2 (40.0%)
From: johannesdahse at gmx dot de Assigned: nikic (profile)
Status: Closed Package: PHP options/info functions
PHP Version: 5.5.13 OS:
Private report: No CVE-ID: None
 [2014-06-03 14:42 UTC] johannesdahse at gmx dot de
Description:
------------
The memory usage functions memory_get_usage() and memory_get_peak_usage() are limited by INTEGER and can only store the memory usage of 2.147 GB. This results in false reports for higher memory usage.

Tested on Windows 7 64bit, PHP 5.5.13 (64bit and 32bit version).

Test script:
---------------
ini_set("memory_limit", "3G");

$a = array();
for($i=0; $i<22; $i++)
	$a[$i] = str_repeat("A", 100000000);

echo memory_get_peak_usage();

Expected result:
----------------
2200227928

Actual result:
--------------
-2094739216

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-05 23:18 UTC] yohgaki@php.net
-Type: Bug +Type: Feature/Change Request
 [2014-07-05 23:18 UTC] yohgaki@php.net
I don't use windows often.
AFAIK, int is 32bit under windows regardless of architecture. Therefore, overflow is spec.

Is there any clean/feasible solution? Anyone?
 [2014-08-26 12:19 UTC] arie_dejong at msn dot com
You can increase the limit by using the negative integer.

$bytes = memory_get_peak_usage();
if($bytes < 0){
	$bytes = PHP_INT_MAX + ($bytes - ~PHP_INT_MAX);
}
 [2015-11-05 08:41 UTC] samuel dot chemla at orange dot com
Hi,

any new on this ?

memory_get_peak_usage() should return an int, which should be 64bit on 64bit systems, but it appears to be 32bit.

Any reason why ?

NB: I'm on PHP 5.5.30 x64 build on Windows 7 x64
 [2015-11-05 09:01 UTC] samuel dot chemla at orange dot com
By the way this shoud be a bug, not a feature request...
 [2017-03-16 20:24 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-03-16 20:24 UTC] nikic@php.net
PHP 7 supports 64bit integers on LLP64 platforms. Of course you'll still get an overflow on 32bit builds, but there isn't much we can do about that.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun May 24 22:01:25 2020 UTC