Skip to content

Stdlib: preg_match() compile failure leaves matches [] not null (#17584) - #17623

Merged
PurHur merged 1 commit into
masterfrom
agent/issue-17584-preg-match-failure-matches
Jul 10, 2026
Merged

PurHur merged 1 commit into
masterfrom
agent/issue-17584-preg-match-failure-matches

Conversation

@PurHur

@PurHur PurHur commented Jul 10, 2026

Copy link
Copy Markdown
Owner

Summary

  • When preg_match() / preg_match_all() pattern compilation fails, Zend leaves the $matches out-parameter as an empty array [].
  • VM wrappers were calling $target->null() on failure, so maintainer_gap_preg_match_duplicate_named.php failed even though compile rejection and preg_last_error() were already correct.

Root cause

ext/standard/preg_match.php and preg_match_all.php wrote null into the by-ref $matches slot on false return; php-src keeps the caller's empty array unchanged.

php-src reference

  • ext/pcre/php_pcre.c — php_pcre_pcre2_compile() duplicate-name handling without PCRE2_DUPNAMES

PHP implementation

  • ext/standard/preg_match.php, ext/standard/preg_match_all.php — write empty HashTable on compile failure

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro/maintainer_gap_preg_match_duplicate_named.php
php bin/vm.php test/repro/maintainer_gap_preg_match_duplicate_named.php
vendor/bin/phpunit --filter VmPregPatternTest
'
ok
repro:0
OK (9 tests, 18 assertions)

Closes #17584

Made with Cursor

Zend keeps the $matches out-parameter as an empty array when pattern
compilation fails; the VM wrapper was writing null and broke parity
probes for duplicate named subpatterns without PCRE2_DUPNAMES.

Co-authored-by: Cursor <cursoragent@cursor.com>
@PurHur
PurHur merged commit ed0e44c into master Jul 10, 2026
@PurHur
PurHur deleted the agent/issue-17584-preg-match-failure-matches branch July 10, 2026 14:20
PurHur added a commit that referenced this pull request Jul 10, 2026
…17673)

Regression from #17623: Zend does not write the by-ref $matches out-parameter
on pattern compile failure; only successful match paths populate it.

Co-authored-by: PurHur <PurHur@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression: preg_match()/preg_replace() duplicate named subpatterns match — compile must fail (re-#14880, ext/pcre/php_pcre.c)

1 participant