Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use of negative numbers in math equations. #903

Merged
merged 1 commit into from
Sep 14, 2023
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.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
- Registered output filters wouldn't run [#899](https://github.com/smarty-php/smarty/issues/899)
- Use of negative numbers in {math} equations [#895](https://github.com/smarty-php/smarty/issues/895)

### Removed
- Removed `$smarty->registered_filters` array
Expand Down
2 changes: 1 addition & 1 deletion src/FunctionHandler/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function handle($params, Template $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 @@ -93,6 +93,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