From: bens at effortlessis dot com Operating system: Fedora Core 1 Linux PHP version: 4.3.6 PHP Bug Type: *Regular Expressions Bug description: preg_match has severe memory leak
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 bug report at http://bugs.php.net/?id=28513&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=28513&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=28513&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=28513&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=28513&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=28513&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=28513&r=needscript Try newer version: http://bugs.php.net/fix.php?id=28513&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=28513&r=support Expected behavior: http://bugs.php.net/fix.php?id=28513&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=28513&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=28513&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=28513&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=28513&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=28513&r=dst IIS Stability: http://bugs.php.net/fix.php?id=28513&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=28513&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=28513&r=float