|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48416 Memleak in ereg() and eregi() functions
Submitted: 2009-05-28 16:57 UTC Modified: 2009-05-30 11:55 UTC
From: bmorel at ssi dot fr Assigned:
Status: Closed Package: Regexps related
PHP Version: 5.2.9 OS: CentOS 5.3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: bmorel at ssi dot fr
New email:
PHP Version: OS:


 [2009-05-28 16:57 UTC] bmorel at ssi dot fr
When repeating an ereg() or eregi() on different patterns, the memory usage of httpd (or php if executed from the command-line) gets huge (about 1GB for the example below) for the time the script executes, then the memory is freed.
During this time, the memory_get_usage() stays normal, and php's memory limit doesn't trigger.

When repeating the ereg() with the same pattern each time, the memory usage stays normal; this happens only when repeating with a different pattern each time.

Verified on CentOS with PHP 5.2.6 & 5.2.9
Verified on Windows XP with PHP 5.2.8

Reproduce code:
for ($i=0;$i<1000000;$i++) ereg("$i","test");

Expected result:
Memory usage staying low.

Actual result:
Memory usage as shown by "top -d 1" gets huge.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-28 17:30 UTC]
Please try using this CVS snapshot:
For Windows:

Pretty sure this is due to the regular expression cache we have, each unique regular expression is cached so we don't keep compiling it but there is no cap on the number we store.

Can you try a 5.3 snapshot, I think we switched from using ereg to pcre with 5.3.
 [2009-05-28 18:17 UTC] bmorel at ssi dot fr
Sorry, same problem with this snapshot (PHP 5.3.0RC3-dev : php5.3-200905281630).

The cache seems to be a good explanation, but it this a good idea ?
I have millions of ereg()s done when I parse huge XML files (several GB), and my memory usage goes too high. I had to replace all my ereg()s with preg_match()s.

 [2009-05-28 21:58 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

Added a LRU and a cache limit to the ereg code for 5.3.

Give it a few hours and grab another 5.3 snapshot, not sure about fixing this in 5.2. Will check with ilia.
 [2009-05-29 00:06 UTC]
Fixed in 5.2 as well now.
 [2009-05-29 21:39 UTC] bmorel at ssi dot fr
Thanks, but the problem is still there in the latest snapshots.
Just verified it on :


Memory usage still gets so high !
 [2009-05-29 23:14 UTC]
I just downloaded a snapshot of 5.2 to check and its definitely fixed.

Memory peaks at 4mb, you must be doing something wrong.
 [2009-05-30 11:55 UTC] bmorel at ssi dot fr
My mistake, I didn't use the correct php binary.
Successfully checked PHP 5.2, 5.3 and 6.0
Sorry and thank you for your fix !
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 23 09:01:32 2024 UTC