Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.29.0 (2026-XX-XX)

* Fix the `matches` operator silently treating PCRE execution errors as non-matches
* Add the `HtmlExtension::htmlAttrValue()` method to resolve a single HTML attribute value the way the `html_attr` function renders it
* Fix `html_attr` JSON encoding a `Stringable` value in a `data-*` attribute instead of using its string representation
* Add documentation comments to attach metadata to nodes (experimental)
Expand Down
8 changes: 6 additions & 2 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ public static function compare($a, $b)
}

/**
* @throws RuntimeError When an invalid pattern is used
* @throws RuntimeError When the regular expression cannot be evaluated
*
* @internal
*/
Expand All @@ -1198,7 +1198,11 @@ public static function matches(string $regexp, ?string $str): int
throw new RuntimeError(\sprintf('Regexp "%s" passed to "matches" is not valid', $regexp).substr($m, 12));
});
try {
return preg_match($regexp, $str ?? '');
if (false === $result = preg_match($regexp, $str ?? '')) {
throw new RuntimeError(\sprintf('Regexp "%s" passed to "matches" failed: %s.', $regexp, preg_last_error_msg()));
}

return $result;
} finally {
restore_error_handler();
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/expressions/matches_error_execution.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Twig reports PCRE execution failures for the "matches" operator
--TEMPLATE--
{{ subject matches regexp }}
--DATA--
return ['subject' => 'aX', 'regexp' => '~(*LIMIT_MATCH=1)^(a+)+$~'];
--EXCEPTION--
Twig\Error\RuntimeError: Regexp "~(*LIMIT_MATCH=1)^(a+)+$~" passed to "matches" failed: Backtrack limit exhausted in "index.twig" at line 2.
Loading