php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77726 Support NULs in PCRE patterns
Submitted: 2019-03-12 07:42 UTC Modified: 2022-06-17 17:37 UTC
From: 605036592 at qq dot com Assigned: cmb (profile)
Status: Closed Package: PCRE related
PHP Version: 7.2.16 OS: CentOS-7.4-x86_64
Private report: No CVE-ID: None
 [2019-03-12 07:42 UTC] 605036592 at qq dot com
Description:
------------
when I use preg_match pattern 0x0, Warning: preg_match(): Null byte in regex
regex has match all character, why php not allow 0x0? it's a bug or has any reason? 

Test script:
---------------
preg_match("/a".chr(0)."bc/", 'abc');

Expected result:
----------------
I expect preg_match right return 0 or 1 and not warning

Actual result:
--------------
Warning: preg_match(): Null byte in regex

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-12 08:15 UTC] requinix@php.net
-Summary: Warning: preg_match(): Null byte in regex +Summary: PCRE does not support NULs in patterns; PHP will warn -Type: Bug +Type: Documentation Problem -Package: PHP Language Specification +Package: PCRE related
 [2019-03-12 08:15 UTC] requinix@php.net
PCRE does not support NUL bytes inside pattern strings.

While not a major pain point, this should probably be documented. The introduction says they can't be delimiters, and the escape sequences page implies it when it mentions non-printing characters, but besides in the actual pcre.org docs linked to with a vague "some limitations" warning, I didn't see anywhere else that conclusively stated it.
 [2019-03-12 17:22 UTC] cmb@php.net
PCRE2 supports NUL bytes in patterns, so we may want to lift the
current restriction.  OTOH,

  preg_match('/a\\0bc/', 'abc')

already works as expected.
 [2019-03-13 04:16 UTC] 605036592 at qq dot com
hello cmb@php.net, I think "/a".chr(0)."bc/" != '/a\\0bc/', so not the same question!
 [2019-03-13 04:19 UTC] requinix@php.net
-Summary: PCRE does not support NULs in patterns; PHP will warn +Summary: PCRE2 now supports NULs in patterns -Type: Documentation Problem +Type: Bug
 [2019-03-13 04:19 UTC] requinix@php.net
I assume the restriction is purely technical, so if we can lift it with PCRE2 then that's probably the right answer.
 [2020-10-27 15:19 UTC] cmb@php.net
> I assume the restriction is purely technical, […]

It seems so.  Some code in pcre_get_compiled_regex_cache_ex() is
not binary safe (i.e. it assumes that a NUL byte terminates the
pattern), and there may be further modifications needed to
php_pcre.c.
 [2020-10-27 15:21 UTC] cmb@php.net
-Summary: PCRE2 now supports NULs in patterns +Summary: PCRE2 does not support NULs in patterns
 [2021-02-18 02:18 UTC] antonino dot spampinato86 at gmail dot com
Null pattern nice job https://3v4l.org/LluRm
 [2022-02-17 22:30 UTC] tobil4sk at outlook dot com
PR to address this bug:
https://github.com/php/php-src/pull/8114
 [2022-02-21 11:16 UTC] tobil4sk at outlook dot com
The following pull request has been associated:

Patch Name: Fix #77726: Allow null character in regex patterns
On GitHub:  https://github.com/php/php-src/pull/8114
Patch:      https://github.com/php/php-src/pull/8114.patch
 [2022-03-04 15:43 UTC] cmb@php.net
-Summary: PCRE2 does not support NULs in patterns +Summary: Support NULs in PCRE patterns -Type: Bug +Type: Feature/Change Request -Assigned To: +Assigned To: cmb
 [2022-06-17 17:37 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2022-06-17 17:37 UTC] cmb@php.net
tobil4sk 's PR has been merged, so I'm closing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC