Skip to content

Commit

Permalink
fix(math): fix equation regexp
Browse files Browse the repository at this point in the history
Fixes #702.
  • Loading branch information
caugner authored and wisskid committed Jan 17, 2022
1 parent 273f7e0 commit b263091
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/plugins/function.math.php
Expand Up @@ -70,7 +70,7 @@ function smarty_function_math($params, $template)
$number = '(?:\d+(?:[,.]\d+)?|pi|π)'; // What is a number
$functionsOrVars = '((?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*))';
$operators = '[+\/*\^%-]'; // Allowed math operators
$regexp = '/^(('.$number.'|'.$functionsOrVars.'|('.$functionsOrVars.'\s*\((?1)+\)|\((?1)+\)))(?:'.$operators.'(?2))?)+$/';
$regexp = '/^(('.$number.'|'.$functionsOrVars.'|('.$functionsOrVars.'\s*\((?1)+\)|\((?1)+\)))(?:'.$operators.'(?1))?)+$/';

if (!preg_match($regexp, $equation)) {
trigger_error("math: illegal characters", E_USER_WARNING);
Expand Down
8 changes: 8 additions & 0 deletions tests/UnitTests/TemplateSource/ValueTests/Math/MathTest.php
Expand Up @@ -44,6 +44,14 @@ public function testFunction()
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}

public function testMultipleOperators()
{
$this->smarty->disableSecurity();
$expected = "2 -- 2";
$tpl = $this->smarty->createTemplate('eval:{$x = 5}{$y = 4}{math equation="x - y + 1" x=$x y=$y} -- {math equation="5 - 4 + 1"}');
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}

public function testSyntaxSin()
{
$this->smarty->disableSecurity();
Expand Down

0 comments on commit b263091

Please sign in to comment.