php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42158 SimpleXMLElement as parameter for preg_match leads to memory leak
Submitted: 2007-07-31 13:13 UTC Modified: 2007-08-20 08:07 UTC
Votes:7
Avg. Score:4.0 ± 0.8
Reproduced:7 of 7 (100.0%)
Same Version:3 (42.9%)
Same OS:5 (71.4%)
From: chabrol at vfnm dot de Assigned:
Status: Wont fix Package: PCRE related
PHP Version: 5CVS-2007-07-31 (snap) OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-07-31 13:13 UTC] chabrol at vfnm dot de
Description:
------------
If you use a SimpleXMLElement as parameter for preg_match or preg_match_all, memory is lost.

P.S.: If you cast the parameter explicit with (string)$xml->name memory usage doesn't increase. So i suppose it's an problem related to internal casting.

Reproduce code:
---------------
<?php

$xml = simplexml_load_string("<root><name>foo</name></root>");
for ($i=1; $i<=20; $i++) {
  preg_match("/bar/", $xml->name);
  // or preg_match_all("/bar/", $xml->name, $matches);
  echo "Memory usage after $i iterations: " . memory_get_usage() . "\n";
}

?>


Expected result:
----------------
I would expect an more or less equal memory usage.

Actual result:
--------------
Memory usage is constantly increasing until hitting configured memory limit

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-31 13:19 UTC] chabrol at vfnm dot de
little imprecision fix: Memory is only increasing until hitting configured memory if you replace the limited for-loop by a while(true) ...
 [2007-07-31 23:04 UTC] judas dot iscariote at gmail dot com
There is no memory leak, those leaks are reported at script shutdown by the Zend Engine.

to fix your problem do this

 preg_match("/bar/", (string)$xml->name);


yes, cast it to string.
 [2007-08-01 07:04 UTC] ibapty at gmail dot com
I can vouch for the workaround based on a similar issue (see http://bugs.php.net/bug.php?id=41911) I encountered. Unfortunately that bug got marked as bogus with a similar explaination. 

I accept that in 99% of uses of PHP where script shutdown occurs that this type of memory leak is acceptable. However, in my case I am using PHP persistently with the win32service module (php.net/win32service). In the end I have had to setup a routine to restart the service every few minutes to workaround this type of memory leak instead of trying to track them all down.
 [2007-08-01 07:55 UTC] chabrol at vfnm dot de
Hello Judas!

In my case it's an long-term running script (using php-cli). So script shutdown doesn't occur timely. Casting to string looks for me like junst an workaround because

1) other functions like ereg doesn't have the same problem. Try it with
ereg("/bar/", $xml->name);
instead of
preg_match("/bar/", $xml->name);

2) if the engine expects an string and therefore cast the value internally, why should this behave different than casting manually in sight of memory usage?

Best regards
Daniel Chabrol
 [2007-08-02 15:14 UTC] chabrol at vfnm dot de
Same effect with PHP 6.0.0-dev (php6.0-200708021030.tar.bz2)
 [2007-08-17 13:42 UTC] jani@php.net
PHP was never designed for 'long time run'. If you do it, you're totally on your own. And this win32service is PECL thing so any bugs with it should be reported at http://pecl.php.net/bugs/
 [2007-08-20 08:07 UTC] chabrol at vfnm dot de
But if it's just a general PHP design problem, why does 
ereg("/bar/", $xml->name);
behaves different than 
preg_match("/bar/", $xml->name);
regarding memory-usage?
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Jul 13 00:01:25 2020 UTC