php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72688 preg_match missing group names in matches
Submitted: 2016-07-27 15:19 UTC Modified: 2016-07-27 16:36 UTC
From: konrad dot baumgart at moneyhouse dot de Assigned: cmb (profile)
Status: Closed Package: PCRE related
PHP Version: 7.0.9 OS: Ubuntu 16.04
Private report: No CVE-ID: None
 [2016-07-27 15:19 UTC] konrad dot baumgart at moneyhouse dot de
Description:
------------
When I create many named groups in pattern like

\(?'group0'foo)|(?'group1'bar)|…\

then the resulting $matches does not show 'group254'.
What was supposed to be in `$matches['group254']` is in `$matches['group255']`, and so on.

the lack repeats every ~255 named groups and the offset is getting worse and worse.

I tested it on php 7 and php 5.5, locally and in online-php-sandbox.

Test script:
---------------
<?php
$pattern = [];
for ($i = 0; $i < 300; $i++) {
    $pattern[] = "(?'group{$i}'{$i}$)";
}

$fullPattern = '/' . implode('|', $pattern) . '/uix';

preg_match($fullPattern, '290', $matches);
var_dump(isset($matches['group253']));
var_dump(isset($matches['group254']));
var_dump(isset($matches['group255']));

var_dump($matches['group290']);
var_dump($matches['group291']);

Expected result:
----------------
bool(true)
bool(true)
bool(true)
string(3) "290"
string(0) ""

Actual result:
--------------
bool(true)
bool(false)
bool(true)
string(0) ""
string(3) "290"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-27 16:36 UTC] cmb@php.net
-Status: Open +Status: Analyzed -Assigned To: +Assigned To: cmb
 [2016-07-27 16:36 UTC] cmb@php.net
Confirmed: <https://3v4l.org/sJWmA>.

The solution is simple: we have to multiply by 0x100 instead of 0xff to
construct a 16bit value[1].

Anyhow, the expected result would be:

    bool(true)
    bool(true)
    bool(true)
    string(3) "290"
    string(0) NULL

see <https://3v4l.org/5CR74>.

[1] <https://github.com/php/php-src/blob/PHP-7.0.9/ext/pcre/php_pcre.c#L264>
 [2016-07-27 17:15 UTC] cmb@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=315c0536c20619478fc548b782132cd65286018f
Log: Fix #72688: preg_match missing group names in matches
 [2016-07-27 17:15 UTC] cmb@php.net
-Status: Analyzed +Status: Closed
 [2016-10-17 10:10 UTC] bwoebi@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=315c0536c20619478fc548b782132cd65286018f
Log: Fix #72688: preg_match missing group names in matches
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 05:01:29 2024 UTC