php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53825 incorrect result preg_replace with large strings
Submitted: 2011-01-23 21:28 UTC Modified: 2011-01-24 04:09 UTC
From: mikhail dot v dot gavrilov at gmail dot com Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.3.5 OS: Ubuntu 10.10
Private report: No CVE-ID: None
 [2011-01-23 21:28 UTC] mikhail dot v dot gavrilov at gmail dot com
Description:
------------
While converting phpBB code to html, preg_replace return empty string.

Test case:
<?php
	$str = '[b]';
	// Please try run this test case also with $i<99997 and compare result's
	for ($i=0; $i<99998; $i++) $str .= 'q';
	$str = preg_replace("#\[b\](.+?)\[\/b\]#is", "[p]", $str);
	echo $str;
?>

I know about pcre.backtrack_limit, but i think in this case PCRE must generate 
exception with fatal error.

Test script:
---------------
<?php
	$str = '[b]';
	// Please try run this test case also with $i<99997 and compare result's
	for ($i=0; $i<99998; $i++) $str .= 'q';
	$str = preg_replace("#\[b\](.+?)\[\/b\]#is", "[p]", $str);
	echo $str;
?>

Expected result:
----------------
Return non empty string or generate exception with fatal error

Actual result:
--------------
Empty string

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-23 22:46 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2011-01-23 22:46 UTC] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Check out what preg_last_error() has returned to figure out why PCRE lib has failed.

You need to increase the pcre.backtrack_limit value.

e.g. ini_set('pcre.backtrack_limit', PHP_INT_MAX);

- http://docs.php.net/manual/en/pcre.configuration.php
- http://docs.php.net/manual/en/function.preg-last-error.php
 [2011-01-23 22:47 UTC] felipe@php.net
-Package: Scripting Engine problem +Package: PCRE related
 [2011-01-24 04:09 UTC] mikhail dot v dot gavrilov at gmail dot com
Probably a rhetorical question and no response will not. Why not make an 
exception for this case? Who does not know would see right away the error, the 
others would put try {} catch {}.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC