php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77827 preg_match does not ignore \r in regex flags
Submitted: 2019-03-30 17:49 UTC Modified: 2019-03-31 11:13 UTC
From: peehaa@php.net Assigned: cmb (profile)
Status: Closed Package: PCRE related
PHP Version: 7.3.3 OS: Windows
Private report: No CVE-ID: None
 [2019-03-30 17:49 UTC] peehaa@php.net
Description:
------------
When running the repro script on my machine I am hitting two issues:

1. preg_match is unable to handle my valid expression
2. it outputs a broken error message

This only happens on my (Windows) machine. On 3v4l (https://3v4l.org/hdiM4) it's working like it should.

Test script:
---------------
<?php

$pattern = '
    ~^
        (?:
            (?:(?P<function>function)\s*\(\s*
                (?:(?P<table1>.+)(?:\.))?(?P<column1>[^ ]+)
            \s*\))
            |
            (?:(?P<table2>.+)(?:\.))?(?P<column2>[^ ]+)
        )
        (?:\s+as\s+(?P<alias>.+))?
    $~ix
';

var_dump(PCRE_VERSION);
var_dump(preg_match($pattern, 'id'));
var_dump(preg_last_error());


Expected result:
----------------
string(16) "10.32 2018-09-10"
int(1)
int(0)


Actual result:
--------------
string(16) "10.32 2018-09-10"
' in \path\to\php-bug.php on line 17
bool(false)
int(1)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-30 19:37 UTC] peehaa@php.net
Weirdly enough when I use a single line pattern it starts working:

<?php

$pattern = '~^(?:(?:(?P<function>function)\s*\(\s*(?:(?P<table1>.+)(?:\.))?(?P<column1>[^ ]+)\s*\))|(?:(?P<table2>.+)(?:\.))?(?P<column2>[^ ]+))(?:\s+as\s+(?P<alias>.+))?$~ix';

var_dump(PCRE_VERSION);
var_dump(preg_match($pattern, 'id'));
var_dump(preg_last_error());

-----

string(16) "10.32 2018-09-10"
int(1)
int(0)

Am I doing something wrong here in combination with the platform I am on somehow?
 [2019-03-30 19:43 UTC] requinix@php.net
Works for my 7.3.3 on Windows. Does it work if you copy/paste the code from this bug report? Is there maybe a stray unprintable character in your file?
 [2019-03-30 20:16 UTC] peehaa@php.net
Nope. Same result. When I check a hex dump of my file I also do not see anything strange at first sight.
 [2019-03-30 20:32 UTC] requinix@php.net
-Summary: preg_match throws error on valid expression +Summary: preg_match does not ignore \r in regex flags -Status: Open +Status: Verified
 [2019-03-30 20:32 UTC] requinix@php.net
Found it. The problem is the line ending in the flags part: PHP will ignore \n but not \r, then complain about it as an invalid flag.
https://github.com/php/php-src/blob/PHP-7.3.3/ext/pcre/php_pcre.c#L696

When I tested, I had created the file with LF line endings.

What's more, the error message is confusing because it says this (with Xdebug) in cmd, where the \r causes a carriage return and so overwrites the rest of the message:

  string(16) "10.32 2018-09-10"

  ' in C:\Users\xxxxxx\t.php on line 17er '

  Call Stack:
      0.4049     397800   1. {main}() C:\Users\xxxxxx\t.php:0
      0.4051     397832   2. preg_match() C:\Users\xxxxxx\t.php:17

  C:\Users\xxxxxx\t.php:17:
  bool(false)
  C:\Users\xxxxxx\t.php:18:
  int(1)

The error message was actually

  Warning: preg_match(): Unknown modifier ''

with an \r between the apostrophes.
 [2019-03-30 20:44 UTC] peehaa@php.net
Awesome! Thanks for digging into it and confirming I am not going entirely crazy yet.
 [2019-03-31 11:13 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2019-03-31 11:37 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=88460c017a06ce8865f1adcc8f70e511cf776e06
Log: Fix #77827: preg_match does not ignore \r in regex flags
 [2019-03-31 11:37 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2019-03-31 11:38 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=88460c017a06ce8865f1adcc8f70e511cf776e06
Log: Fix #77827: preg_match does not ignore \r in regex flags
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC