php / php/php-src

preg_match and preg_match_all both do not set $matches on regex fail, leaving as it was set prior to call

Open
#7,917 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Extension: pcre Feature Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php // preg_match_all.php
$matches = [0 => 'Orange',
            1 => 'Apple',
            2 => 'Banana',
            3 => 'Monkey'];
$result = preg_match_all('aeiou{0,', 'ou', $matches);
var_dump($result);
var_dump($matches);

$result = preg_match('aeiou{0,', 'ou', $matches);
var_dump($result);
var_dump($matches);

Resulted in this output from command line %php preg_match_all.php:

PHP Warning:  preg_match_all(): Delimiter must not be alphanumeric or backslash in preg_match_all.php on line 6
PHP Stack trace:
PHP   1. {main}() preg_match_all.php:0
PHP   2. preg_match_all($pattern = 'aeiou{0,', $subject = 'ou', $subpatterns = [0 => 'Orange', 1 => 'Apple', 2 => 'Banana', 3 => 'Monkey']) preg_match_all.php:6
preg_match_all.php:7:
bool(false)
preg_match_all.php:8:
array(4) {
    [0] =>
  string(6) "Orange"
    [1] =>
  string(5) "Apple"
    [2] =>
  string(6) "Banana"
    [3] =>
  string(6) "Monkey"
}
PHP Warning:  preg_match(): Delimiter must not be alphanumeric or backslash in preg_match_all.php on line 10
PHP Stack trace:
PHP   1. {main}() preg_match_all.php:0
PHP   2. preg_match($pattern = 'aeiou{0,', $subject = 'ou', $subpatterns = [0 => 'Orange', 1 => 'Apple', 2 => 'Banana', 3 => 'Monkey']) preg_match_all.php:10
preg_match_all.php:11:
bool(false)
preg_match_all.php:12:
array(4) {
    [0] =>
  string(6) "Orange"
    [1] =>
  string(5) "Apple"
    [2] =>
  string(6) "Banana"
    [3] =>
  string(6) "Monkey"
}

But I expected this output instead:

PHP Warning:  preg_match_all(): Delimiter must not be alphanumeric or backslash in preg_match_all.php on line 6
PHP Stack trace:
PHP   1. {main}() preg_match_all.php:0
PHP   2. preg_match_all($pattern = 'aeiou{0,', $subject = 'ou', $subpatterns = [0 => 'Orange', 1 => 'Apple', 2 => 'Banana', 3 => 'Monkey']) preg_match_all.php:6
preg_match_all.php:7:
bool(false)
preg_match_all.php:8:
either: null or array[0]
PHP Warning:  preg_match(): Delimiter must not be alphanumeric or backslash in preg_match_all.php on line 10
PHP Stack trace:
PHP   1. {main}() preg_match_all.php:0
PHP   2. preg_match($pattern = 'aeiou{0,', $subject = 'ou', $subpatterns = [0 => 'Orange', 1 => 'Apple', 2 => 'Banana', 3 => 'Monkey']) preg_match_all.php:10
preg_match_all.php:11:
bool(false)
preg_match_all.php:12:
either: null or array[0]
PHP Version

PHP 7.4.27

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the provided preg_match() and preg_match_all() calls with the invalid pattern on PHP 7.4.27, then trace the two APIs in the PHP interpreter. Done means a failed match no longer leaves the caller's previous $matches contents unchanged, with regression coverage for both functions.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.