Skip to content

Commit 3d857d5

Browse files
authored
round(): Validate the rounding mode (#12252)
1 parent f6fae19 commit 3d857d5

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ Standard:
1515
. Implement GH-12188 (Indication for the int size in phpinfo()). (timwolla)
1616
. Partly fix GH-12143 (Incorrect round() result for 0.49999999999999994).
1717
(timwolla)
18+
. Fix GH-12252 (round(): Validate the rounding mode). (timwolla)
1819

1920
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

UPGRADING

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ PHP 8.4 UPGRADE NOTES
5858
would have resulted in 1.0 instead of the correct result 0.0. Additional
5959
inputs might also be affected and result in different outputs compared to
6060
earlier PHP versions.
61+
. round() now validates the value of the $mode parameter and throws a ValueError
62+
for invalid modes. Previously invalid modes would have been interpreted as
63+
PHP_ROUND_HALF_UP.
6164

6265
========================================
6366
6. New Functions

ext/standard/math.c

+11
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,17 @@ PHP_FUNCTION(round)
335335
}
336336
}
337337

338+
switch (mode) {
339+
case PHP_ROUND_HALF_UP:
340+
case PHP_ROUND_HALF_DOWN:
341+
case PHP_ROUND_HALF_EVEN:
342+
case PHP_ROUND_HALF_ODD:
343+
break;
344+
default:
345+
zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)");
346+
RETURN_THROWS();
347+
}
348+
338349
switch (Z_TYPE_P(value)) {
339350
case IS_LONG:
340351
/* Simple case - long that doesn't need to be rounded. */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
round() rejects invalid rounding modes.
3+
--FILE--
4+
<?php
5+
try {
6+
var_dump(round(1.5, mode: 1234));
7+
} catch (ValueError $e) {
8+
echo $e->getMessage(), PHP_EOL;
9+
}
10+
?>
11+
--EXPECT--
12+
round(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*)

0 commit comments

Comments
 (0)