From 8d3a7af308ba994e99e19af1d657dcf9772d26e5 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Tue, 18 Jan 2022 13:57:58 +0100 Subject: [PATCH 1/4] Cherry-picked from da76d927ed77fb6b3cb8345b83776712d0778e55 Co-Authored-By: Thomas BACCELLI --- libs/sysplugins/smarty_internal_compile_function.php | 2 +- .../smarty_internal_config_file_compiler.php | 2 +- libs/sysplugins/smarty_internal_runtime_codeframe.php | 2 +- .../smarty_internal_templatecompilerbase.php | 2 +- libs/sysplugins/smartycompilerexception.php | 10 +++++----- .../_shared/CacheResourceTestCommon.php | 3 ++- .../ResourceTests/Stream/StreamResourceTest.php | 2 +- tests/UnitTests/SecurityTests/SecurityTest.php | 2 +- .../ValueTests/Variables/Stream/StreamVariableTest.php | 2 +- 9 files changed, 14 insertions(+), 13 deletions(-) diff --git a/libs/sysplugins/smarty_internal_compile_function.php b/libs/sysplugins/smarty_internal_compile_function.php index d0f2b0f4a..84e9584d9 100644 --- a/libs/sysplugins/smarty_internal_compile_function.php +++ b/libs/sysplugins/smarty_internal_compile_function.php @@ -157,7 +157,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler) $output = "template->compiled->nocache_hash}%%*/smarty->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n"; $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>"; - $output .= "template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n"; + $output .= "template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash ?? '', ob_get_clean());\n"; $output .= "}\n}\n"; $output .= "/*/ {$_funcName}_nocache */\n\n"; $output .= "?>\n"; diff --git a/libs/sysplugins/smarty_internal_config_file_compiler.php b/libs/sysplugins/smarty_internal_config_file_compiler.php index 90c5dcefa..a9b940e5a 100644 --- a/libs/sysplugins/smarty_internal_config_file_compiler.php +++ b/libs/sysplugins/smarty_internal_config_file_compiler.php @@ -158,7 +158,7 @@ public function compileTemplate(Smarty_Internal_Template $template) } // template header code $template_header = - "template->source->filepath}' */ ?>\n"; $code = 'smarty->ext->configLoad->_loadConfigVars($_smarty_tpl, ' . diff --git a/libs/sysplugins/smarty_internal_runtime_codeframe.php b/libs/sysplugins/smarty_internal_runtime_codeframe.php index 983ca6180..b5361c9bb 100644 --- a/libs/sysplugins/smarty_internal_runtime_codeframe.php +++ b/libs/sysplugins/smarty_internal_runtime_codeframe.php @@ -45,7 +45,7 @@ public function create( $properties[ 'cache_lifetime' ] = $_template->cache_lifetime; } $output = "source->filepath) . "' */\n\n"; $output .= "/* @var Smarty_Internal_Template \$_smarty_tpl */\n"; $dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' . diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 6a8dc9c48..272616148 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -1135,7 +1135,7 @@ public function trigger_template_error($args = null, $line = null, $tagline = nu flush(); } $e = new SmartyCompilerException($error_text); - $e->line = $line; + $e->setLine($line); $e->source = trim(preg_replace('![\t\r\n]+!', ' ', $match[ $line - 1 ])); $e->desc = $args; $e->template = $this->template->source->filepath; diff --git a/libs/sysplugins/smartycompilerexception.php b/libs/sysplugins/smartycompilerexception.php index f7ad39b93..8833aa52c 100644 --- a/libs/sysplugins/smartycompilerexception.php +++ b/libs/sysplugins/smartycompilerexception.php @@ -16,12 +16,12 @@ public function __toString() } /** - * The line number of the template error - * - * @type int|null + * @param int $line */ - public $line = null; - + public function setLine($line) + { + $this->line = $line; + } /** * The template source snippet relating to the error * diff --git a/tests/UnitTests/CacheResourceTests/_shared/CacheResourceTestCommon.php b/tests/UnitTests/CacheResourceTests/_shared/CacheResourceTestCommon.php index 92207d2ad..dc4c1cd7e 100644 --- a/tests/UnitTests/CacheResourceTests/_shared/CacheResourceTestCommon.php +++ b/tests/UnitTests/CacheResourceTests/_shared/CacheResourceTestCommon.php @@ -30,7 +30,8 @@ protected function doClearCacheAssertion($a, $b) public function compiledPrefilter($text, Smarty_Internal_Template $tpl) { - return str_replace('#', $tpl->getTemplateVars('test'), $text); + $replace = $tpl->getTemplateVars('test'); + return str_replace('#', $replace ?? '', $text); } /** diff --git a/tests/UnitTests/ResourceTests/Stream/StreamResourceTest.php b/tests/UnitTests/ResourceTests/Stream/StreamResourceTest.php index 6d0201bd2..402f0226e 100644 --- a/tests/UnitTests/ResourceTests/Stream/StreamResourceTest.php +++ b/tests/UnitTests/ResourceTests/Stream/StreamResourceTest.php @@ -234,7 +234,7 @@ public function stream_write($data) $v = &$GLOBALS[$this->varname]; $l = strlen($data); $p = &$this->position; - $v = substr($v, 0, $p) . $data . substr($v, $p += $l); + $v = substr($v ?? '', 0, $p) . $data . substr($v ?? '', $p += $l); return $l; } diff --git a/tests/UnitTests/SecurityTests/SecurityTest.php b/tests/UnitTests/SecurityTests/SecurityTest.php index 4d1b8a3ab..dfaffa671 100644 --- a/tests/UnitTests/SecurityTests/SecurityTest.php +++ b/tests/UnitTests/SecurityTests/SecurityTest.php @@ -416,7 +416,7 @@ public function stream_write($data) $v = &$GLOBALS[$this->varname]; $l = strlen($data); $p = &$this->position; - $v = substr($v, 0, $p) . $data . substr($v, $p += $l); + $v = substr($v ?? '', 0, $p) . $data . substr($v ?? '', $p += $l); return $l; } diff --git a/tests/UnitTests/TemplateSource/ValueTests/Variables/Stream/StreamVariableTest.php b/tests/UnitTests/TemplateSource/ValueTests/Variables/Stream/StreamVariableTest.php index e49d62058..18560ba82 100644 --- a/tests/UnitTests/TemplateSource/ValueTests/Variables/Stream/StreamVariableTest.php +++ b/tests/UnitTests/TemplateSource/ValueTests/Variables/Stream/StreamVariableTest.php @@ -96,7 +96,7 @@ public function stream_write($data) $v = &$GLOBALS[$this->varname]; $l = strlen($data); $p = &$this->position; - $v = substr($v, 0, $p) . $data . substr($v, $p += $l); + $v = substr($v ?? '', 0, $p) . $data . substr($v ?? '', $p += $l); return $l; } From 277648b81cb49cfd1449bc876f874c2421e97af2 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Tue, 18 Jan 2022 13:59:02 +0100 Subject: [PATCH 2/4] Cherry-picked from da76d927ed77fb6b3cb8345b83776712d0778e55 Co-Authored-By: Thomas BACCELLI --- libs/sysplugins/smarty_internal_cacheresource_file.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/sysplugins/smarty_internal_cacheresource_file.php b/libs/sysplugins/smarty_internal_cacheresource_file.php index abed98d85..c77ae9e17 100644 --- a/libs/sysplugins/smarty_internal_cacheresource_file.php +++ b/libs/sysplugins/smarty_internal_cacheresource_file.php @@ -196,8 +196,8 @@ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $e */ public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) { - clearstatcache(true, $cached->lock_id); - if (is_file($cached->lock_id)) { + clearstatcache(true, $cached->lock_id ?? ''); + if (null !== $cached->lock_id && is_file($cached->lock_id)) { $t = filemtime($cached->lock_id); return $t && (time() - $t < $smarty->locking_timeout); } else { From 323aefa89f32844ba35b9a10cda2a9d003998211 Mon Sep 17 00:00:00 2001 From: Thomas BACCELLI Date: Wed, 20 Oct 2021 16:00:21 +0200 Subject: [PATCH 3/4] Fix phpunit warning --- tests/UnitTests/SecurityTests/SecurityTest.php | 2 +- .../TagTests/TemplateFunction/CompileFunctionTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/UnitTests/SecurityTests/SecurityTest.php b/tests/UnitTests/SecurityTests/SecurityTest.php index dfaffa671..d68ac3d48 100644 --- a/tests/UnitTests/SecurityTests/SecurityTest.php +++ b/tests/UnitTests/SecurityTests/SecurityTest.php @@ -366,9 +366,9 @@ public function testNotTrustedUri() /** * In security mode, accessing $smarty.template_object should be illegal. - * @expectedException SmartyCompilerException */ public function testSmartyTemplateObject() { + $this->expectException(SmartyCompilerException::class); $this->smarty->display('string:{$smarty.template_object}'); } diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/CompileFunctionTest.php b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/CompileFunctionTest.php index ca3e37141..61a085838 100644 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/CompileFunctionTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/CompileFunctionTest.php @@ -435,9 +435,9 @@ public function dataTestSpacing() /** * Test handling of function names that are a security risk - * @expectedException SmartyCompilerException */ public function testIllegalFunctionName() { + $this->expectException(SmartyCompilerException::class); $this->smarty->fetch('string:{function name=\'rce(){};echo "hi";function \'}{/function}'); } From cef8951100ea692d2a34cb3d00516456158ea86a Mon Sep 17 00:00:00 2001 From: Thomas BACCELLI Date: Wed, 20 Oct 2021 16:02:33 +0200 Subject: [PATCH 4/4] Add PHP 8.1 tests --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e82642aa5..21977687c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,6 +28,7 @@ jobs: - "7.3" - "7.4" - "8.0" + - "8.1" compiler: - default