Skip to content

Commit

Permalink
Fix use of negative numbers in math equations. (#904)
Browse files Browse the repository at this point in the history
Fixes #895
  • Loading branch information
wisskid authored Sep 14, 2023
1 parent d6c4274 commit 2ff66e0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- `|strip_tags` does not work if the input is 0 [#890](https://github.com/smarty-php/smarty/issues/890)
- Use of negative numbers in {math} equations [#895](https://github.com/smarty-php/smarty/issues/895)

## [4.3.2] - 2023-07-19

Expand Down
2 changes: 1 addition & 1 deletion libs/plugins/function.math.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function smarty_function_math($params, $template)
$equation = preg_replace('/\s+/', '', $equation);

// Adapted from https://www.php.net/manual/en/function.eval.php#107377
$number = '(?:\d+(?:[,.]\d+)?|pi|π)'; // What is a number
$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.'(?1))?)+$/';
Expand Down
8 changes: 8 additions & 0 deletions tests/UnitTests/TemplateSource/ValueTests/Math/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public function testFunctionFloat()
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}

public function testNegativeNumbers()
{
$this->smarty->disableSecurity();
$expected = "-19 -- 4.1";
$tpl = $this->smarty->createTemplate('eval:{$x = 4}{$y = 5.5}{math equation="-2.0*(x+y)" x=$x y=$y} -- {math equation="-20.5 / -5"}');
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}

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

0 comments on commit 2ff66e0

Please sign in to comment.