ID: 28513
Comment by: R3MOV3CAPSphpbugs at R3MOV3CAPSneodisco dot com
Reported By: bens at effortlessis dot com
Status: Open
Bug Type: *Regular Expressions
Operating System: Fedora Core 1 Linux
PHP Version: 4.3.6
New Comment:
This is a simple, serious, and ugly ugly bug!!! Took me a while to cut
it back, but here is the essence.
<?php
while (1) {
$body = "any string";
$rand = "different strings".mt_rand(0,mt_getrandmax());
$pattern = "/$rand/";
preg_match($pattern, $body, $match);
}
?>
I am using PHP5.0.0 and PHP5.0.01. These 4 lines chew 50MB per second.
Have a nice day.
Previous Comments:
------------------------------------------------------------------------
[2004-05-25 02:11:51] bens at effortlessis dot com
Description:
------------
I wrote a script to parse a sendmail log file to match records together
by message id in order to gather some statistics about a particular
relay situation.
When run, this script quickly swallowed all available RAM, bringing a
busy, production system to a standstill. I reduced the script to the
simplest code that causes the memory leak.
When run as "php -q scriptname.php /var/log/maillog" from the command
line, where maillog contains > 500,000 lines, this script will leak
memory until the system load average exceeds 5.0.
Rem out the preg_match on the innermost loop and the memory leak
disappears. This bug is found in 4.3.4 and 4.3.6, I haven't tested
other versions.
Reproduce code:
---------------
<?
set_time_limit(0);
// GETS $msgid;
$match="/\]\:\s+([0-9a-zA-Z]+)\:/";
if (!$file=trim($_SERVER['argv'][1]))
die('You must specify maillog file location');
if ($fp=fopen($file, 'r'))
{
while ($line=fgets($fp, 1024))
if (preg_match($match, $line, $r))
{
$msgid=trim($r[1]);
//$mfrom="/$msgid\:\s+from=\<*(.*?)[\s\>].*?relay=.*?\[([0-9\.]+)\]/";
$mfrom="/$msgid/";
// REM THIS LINE OUT AND THE MEMORY LEAK STOPS.
preg_match($mfrom, $line);
}
fclose($fp);
}
?>
Expected result:
----------------
The "top" command should show this PHP instance using < 10% of RAM on a
1 GB RAM system.
Actual result:
--------------
The "top" command shows > 99% of memory usage on a 1 GB system.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=28513&edit=1