From 1ee9dae55a41f2d4f110c4fba2c2042bb4a50d56 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 18 Sep 2026 17:15:53 +0100 Subject: [PATCH] Cover and document the matches operator throwing on an invalid UTF-8 subject --- CHANGELOG | 2 +- tests/Fixtures/expressions/matches_error_bad_utf8.test | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/expressions/matches_error_bad_utf8.test diff --git a/CHANGELOG b/CHANGELOG index a949ef23a..201468147 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,7 +12,7 @@ * Fix the sandbox resolving `use` trait templates before checking that the `use` tag is allowed * Fix `html_attr` dropping `style` declarations whose value is `0`, `0.0` or `'0'` * Fix the `default` filter fallback emitting an undefined variable warning when it uses the null-safe operator - * Fix the `matches` operator silently treating PCRE execution errors as non-matches + * Fix the `matches` operator silently treating PCRE execution errors as non-matches; it now throws a `RuntimeError` * Fix `TemplateWrapper::streamBlock()`, `TemplateWrapper::hasBlock()`, and `TemplateWrapper::getBlockNames()` omitting environment globals * Fix exceptions from dynamic parent expressions escaping without template context * Add the `BlockChain` class to compose blocks from multiple templates without using template internals diff --git a/tests/Fixtures/expressions/matches_error_bad_utf8.test b/tests/Fixtures/expressions/matches_error_bad_utf8.test new file mode 100644 index 000000000..cb59b2a30 --- /dev/null +++ b/tests/Fixtures/expressions/matches_error_bad_utf8.test @@ -0,0 +1,8 @@ +--TEST-- +Twig reports a PCRE failure when the "matches" subject is not valid UTF-8 +--TEMPLATE-- +{{ subject matches regexp }} +--DATA-- +return ['subject' => "\xC3\x28abc", 'regexp' => '/^\p{L}+$/u']; +--EXCEPTION-- +Twig\Error\RuntimeError: Regexp "/^\p{L}+$/u" passed to "matches" failed: Malformed UTF-8 characters, possibly incorrectly encoded in "index.twig" at line 2.