8000 bug #34842 [ExpressionLanguage] Process division by zero (tigr1991) · symfony/symfony@2eacbc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2eacbc5

Browse files
bug #34842 [ExpressionLanguage] Process division by zero (tigr1991)
This PR was merged into the 3.4 branch. Discussion ---------- [ExpressionLanguage] Process division by zero | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | To be able to catch the error in expression like ` 1 / 0` **Before PR:** ``` try { 1 / 0; } catch (\Throwable $e) { // It won't be caught anyway // PHP Warning: Division by zero in... } try { 1 % 0; } catch (\Throwable $e) { // It will be caught since PHP7 // \DivisionByZeroError with message `Modulo by zero` } ``` **After PR:** ``` try { 1 / 0; } catch (\Throwable $e) { // It will be caught // \DivisionByZeroError with message `Division by zero` } try { 1 % 0; } catch (\Throwable $e) { // It will be caught // \DivisionByZeroError with message `Modulo by zero` } ``` Commits ------- 02ab72a [ExpressionLanguage][Node][BinaryNode] Process division by zero
2 parents 429b18e + 02ab72a commit 2eacbc5

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,16 @@ public function evaluate($functions, $values)
147147
case '*':
148148
return $left * $right;
149149
case '/':
150+
if (0 == $right) {
151+
throw new \DivisionByZeroError('Division by zero');
152+
}
153+
150154
return $left / $right;
151155
case '%':
156+
if (0 == $right) {
157+
throw new \DivisionByZeroError('Modulo by zero');
158+
}
159+
152160
return $left % $right;
153161
case 'matches':
154162
return preg_match($right, $left);

src/Symfony/Component/ExpressionLanguage/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
],
1818
"require": {
1919
"php": "^5.5.9|>=7.0.8",
20-
"symfony/cache": "~3.1|~4.0"
20+
"symfony/cache": "~3.1|~4.0",
21+
"symfony/polyfill-php70": "~1.6"
2122
},
2223
"autoload": {
2324
"psr-4": { "Symfony\\Component\\ExpressionLanguage\\": "" },

0 commit comments

Comments
 (0)
0