php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44764 preg_match returning empty array on certain string lengths
Submitted: 2008-04-17 18:14 UTC Modified: 2008-04-17 18:36 UTC
From: steemann at globalpark dot de Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 5.2.5 OS: Linux rom 2.4.21-297-smp4G
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: steemann at globalpark dot de
New email:
PHP Version: OS:

 

 [2008-04-17 18:14 UTC] steemann at globalpark dot de
Description:
------------
When passing somewhat long strings to the first two parameters of 
preg_match, the function result reproducibly changes to an empty 
array if the one the strings gets too long.

The reproduce code works on PHP 5.1.4 CLI, but not on PHP 5.2.4 and 
PHP 5.2.5 CLI.
When changing the string length of 1536 into 1535, the code also 
works fine in PHP 5.2.x.

This affects the seconds parameter of preg_match.

If the 1536 in the reproduce code is reduced to 1535, the code will 
work again. But if then the number of iterations in the for loop is 
changed from 64 to 65 iterations, the code will misbehave again 
(returning the empty array).

Seems to be some length overflow somewhere.


Reproduce code:
---------------
$parts=array();
for ($i=1;$i<=64;$i++)
{
  $parts[]="#".$i."#";
}

preg_match("/^(.*)(".implode("|",$parts).")/",$parts[0].str_repeat(" ",1536),$matches);

var_dump($matches);

Expected result:
----------------
array(3) {
  [0]=>
  string(3) "#1#"
  [1]=>
  string(0) ""
  [2]=>
  string(3) "#1#"
}


Actual result:
--------------
array(0) {
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-17 18:36 UTC] felipe@php.net
This is expected. The reason can be known using preg_last_error(), that in the case returns 2. (2 = PREG_BACKTRACK_LIMIT_ERROR)

http://docs.php.net/manual/en/pcre.configuration.php#ini.pcre.backtrack-limit
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 29 04:01:30 2024 UTC