Skip to content

Stdlib: preg_* /u rejects invalid UTF-8 subjects (#17503) - #17512

Merged
PurHur merged 1 commit into
masterfrom
agent/issue-17503-preg-bad-utf8
Jul 9, 2026
Merged

PurHur merged 1 commit into
masterfrom
agent/issue-17503-preg-bad-utf8

Conversation

@PurHur

@PurHur PurHur commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Summary

  • Validate subject UTF-8 when a preg pattern carries the /u modifier (PCRE2_UTF) before match/split/replace, matching Zend ext/pcre/php_pcre.c.
  • Set preg_last_error() to PREG_BAD_UTF8_ERROR (4) and return false/null as appropriate; preg_grep skips bad haystack elements; array preg_replace returns partial results.

php-src reference

  • ext/pcre/php_pcre.c — php_pcre_match_impl() UTF-8 validation / PREG_BAD_UTF8_ERROR

PHP implementation

  • ext/standard/VmPregPure.php — ensureUtf8SubjectForOpts()
  • ext/standard/VmPregPattern.php — PCRE2_UTF constant
  • ext/standard/preg_grep.php, ext/standard/VmPreg.php — Zend partial-array behavior

Verification

./script/docker-exec.sh -- bash -lc '
php bin/vm.php test/repro/maintainer_gap_preg_match_invalid_utf8.php
php bin/vm.php test/repro/maintainer_gap_preg_split_invalid_utf8.php
php bin/vm.php test/compliance/cases/pcre/preg_utf8_invalid_subject.phpt
source script/php-env.sh && vendor/bin/phpunit --filter VmPregUtf8SubjectTest
'

Output:

false
4
false
4
match_fail
4
split_fail
4
replace_null
4
array (
  0 => 'a',
)
4
OK (4 tests, 8 assertions)

Closes #17503

Made with Cursor

Validate subject UTF-8 when pattern carries u (PCRE2_UTF) before match/split/replace,
setting PREG_BAD_UTF8_ERROR like Zend ext/pcre/php_pcre.c. preg_grep skips bad haystack
elements; array preg_replace returns partial results on per-element failure.

Co-authored-by: Cursor <cursoragent@cursor.com>
@PurHur
PurHur merged commit 9998e1f into master Jul 9, 2026
@PurHur
PurHur deleted the agent/issue-17503-preg-bad-utf8 branch July 9, 2026 19:42
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_*() /u on invalid UTF-8 — match succeeds, preg_last_error() stale (ext/pcre/php_pcre.c)

1 participant