PHP PREG and recursions causes crash
Posted by: toxicsyntax (---.dr.dk)
Date: August 10, 2010 07:39PM

I am trying to debug a problem with a wordpress plugin with WAMP, but during my testing another problem have turned up, which does not exist on the Linux server the plugin is normally running on.

What happens is that a regular expression ( "/^\d+(?:,\d+)+$/" ) that is used to match a long string containg comma-separated intergers (like "154,33432,32032,23253,23253,...." - about 3000 characters) will cause the Apache server to restart!

I have tried to mess around with the preg.recursion_limit, and I found that if i reduce this limit to 686 the regex no longer causes Apache to crash, but instead the match fails and PREG_RECURSION_LIMIT_ERROR is returned from preg_last_error().

I am very confused about this, and I have never seen it before - as I noted this only occurs when I test the plugin on a WAMP server, not on the Linux server that normally runs it. From what I can google, what happens is that the regular expression engine recurses so many times that it exhausts the available stack, and then thread crashes - but from my messing with the recursion_limit, this seems to happen already after 686 recursions, which is ridiculously low.

If there is anyone who have seen this behavior before, I would really appreciate a hint on how to fix!

-- regards, Anders

Options: ReplyQuote
Re: PHP PREG and recursions causes crash
Posted by: stevenmartin99 (---.mobile.digiweb.ie)
Date: August 10, 2010 10:28PM

its comma separated or not? and they are intergers?

then are u sure u need to be using a regex function at all for this?

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: PHP PREG and recursions causes crash
Posted by: toxicsyntax (---.ds1-khk.adsl.cybercity.dk)
Date: August 10, 2010 11:35PM

Well, it is a string that will be handled in different ways, depending on what is in it. One of the things that the string may contain is a comma-separated list of numbers, like this "124,4363,232453,2324,23253,...". The string may be very long (like, more than 3000 characters).

I test the string against different regular expressions, and this is where I see the unexpected stack error. I expect I would be able to test for this in some other way, like using explode() and intval() in some combination, but it really intriges me why preg_match causes the apache server to restart?!

As I said, I have narrowed it down to something with recursion limit, as I can get the regular expression to fail with a PREG_RECURSION_LIMIT_ERROR by lowering the preg.recursion_limit (in php.ini) to 686. If it is 687 or higher, the regular expression causes the Apache server to crash and restart.

By the way, this may be a more general problem with Apache or PHP on Windows, as tried to uninstall WAMP and install Apache and PHP separately, and I saw the same error there. It works on my production server, which is Debian Linux, however.

Options: ReplyQuote
Re: PHP PREG and recursions causes crash
Posted by: toumimi (---.163.192-77.rev.gaoland.net)
Date: August 11, 2010 07:13AM

PCRE, which is used with preg_match, has some limitations.
See here to get more informations : [www.pcre.org]

Then, it's possible that on your server, memory size or any other parameter or server specification that allows longer strings or catched strings... So I think this is not a Windows limitation.

Florian

WampServer Patch (Screenshots)
Topic EN : www.wampserver.com
Topic FR : www.wampserver.com

Options: ReplyQuote
Re: PHP PREG and recursions causes crash
Posted by: toxicsyntax (---.dr.dk)
Date: August 11, 2010 10:55AM

Yeah, I think so too - it appears to be related to memory size (well stacksize, really), but I have no idea where to configure these values. As far as I know, I am using default configurations for both PHP and Apache on both the Windows and Debian servers, but there must be some difference, apparently...

Thanks anyways!

-- Anders

Options: ReplyQuote
Re: PHP PREG and recursions causes crash
Posted by: toxicsyntax (---.dr.dk)
Date: August 11, 2010 01:32PM

Well, I asked this question on StackOverflow as well, and it turns out that it is related to this: [bugs.php.net]

I can't get the editbin fix to work though, but I will keep on messing around with it.

Options: ReplyQuote
Re: PHP PREG and recursions causes crash
Posted by: conn (---.9-1.cable.virginm.net)
Date: March 14, 2015 03:52PM

I know this is very old, but did anyone ever work out how to fix this? I am having the exact same issue.

Options: ReplyQuote


Sorry, only registered users may post in this forum.