Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/Extension/DefaultExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,9 @@ public function smarty_modifier_regex_replace($string, $search, $replace, $limit
} else {
$search = $this->regex_replace_check($search);
}
return preg_replace($search, $replace, $string, $limit);
// provide defaults to prevent deprecation errors in PHP >=8.1
// a null $limit falls back to 0, matching PHP's historical null-to-int coercion
return preg_replace($search, $replace ?? '', $string ?? '', $limit ?? 0);
}

/**
Expand All @@ -681,6 +683,8 @@ public function smarty_modifier_regex_replace($string, $search, $replace, $limit
*/
private function regex_replace_check($search)
{
// provide $search default to prevent deprecation errors in PHP >=8.1
$search = $search ?? '';
// null-byte injection detection
// anything behind the first null-byte is ignored
if (($pos = strpos($search, "\0")) !== false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ public function testDefault()
$this->assertEquals("Infertility unlikely to be passed on, experts say.", $this->smarty->fetch($tpl));
}

public function testWithNull()
{
// the test suite mutes E_DEPRECATED globally, so collect deprecations manually
$deprecations = [];
set_error_handler(function ($errno, $errstr) use (&$deprecations) {
$deprecations[] = $errstr;
return true;
}, E_DEPRECATED);

try {
$this->smarty->assign('subject', null);
$tpl = $this->smarty->createTemplate('string:{$subject|regex_replace:"/[\r\t\n]/":" "}');
$this->assertEquals("", $this->smarty->fetch($tpl));

// null replacement removes the match
$this->smarty->assign('replacement', null);
$tpl = $this->smarty->createTemplate('string:{"a\nb"|regex_replace:"/[\r\t\n]/":$replacement}');
$this->assertEquals("ab", $this->smarty->fetch($tpl));

// null limit keeps behaving as 0 (no replacements), as PHP's null-to-int
// coercion did before the deprecation
$this->smarty->assign('limit', null);
$tpl = $this->smarty->createTemplate('string:{"a\nb\nc"|regex_replace:"/[\r\t\n]/":" ":$limit}');
$this->assertEquals("a\nb\nc", $this->smarty->fetch($tpl));
} finally {
restore_error_handler();
}

$this->assertEquals([], $deprecations);
}

public function testUmlauts()
{
$tpl = $this->smarty->createTemplate('string:{"Infertility unlikely tö\näe passed on, experts say."|regex_replace:"/[\r\t\n]/u":" "}');
Expand Down
Loading