mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
round(): Validate the rounding mode (#12252)
This commit is contained in:
1
NEWS
1
NEWS
@@ -15,5 +15,6 @@ Standard:
|
||||
. Implement GH-12188 (Indication for the int size in phpinfo()). (timwolla)
|
||||
. Partly fix GH-12143 (Incorrect round() result for 0.49999999999999994).
|
||||
(timwolla)
|
||||
. Fix GH-12252 (round(): Validate the rounding mode). (timwolla)
|
||||
|
||||
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>
|
||||
|
||||
@@ -58,6 +58,9 @@ PHP 8.4 UPGRADE NOTES
|
||||
would have resulted in 1.0 instead of the correct result 0.0. Additional
|
||||
inputs might also be affected and result in different outputs compared to
|
||||
earlier PHP versions.
|
||||
. round() now validates the value of the $mode parameter and throws a ValueError
|
||||
for invalid modes. Previously invalid modes would have been interpreted as
|
||||
PHP_ROUND_HALF_UP.
|
||||
|
||||
========================================
|
||||
6. New Functions
|
||||
|
||||
@@ -335,6 +335,17 @@ PHP_FUNCTION(round)
|
||||
}
|
||||
}
|
||||
|
||||
switch (mode) {
|
||||
case PHP_ROUND_HALF_UP:
|
||||
case PHP_ROUND_HALF_DOWN:
|
||||
case PHP_ROUND_HALF_EVEN:
|
||||
case PHP_ROUND_HALF_ODD:
|
||||
break;
|
||||
default:
|
||||
zend_argument_value_error(3, "must be a valid rounding mode (PHP_ROUND_*)");
|
||||
RETURN_THROWS();
|
||||
}
|
||||
|
||||
switch (Z_TYPE_P(value)) {
|
||||
case IS_LONG:
|
||||
/* Simple case - long that doesn't need to be rounded. */
|
||||
|
||||
12
ext/standard/tests/math/round_valid_rounding_mode.phpt
Normal file
12
ext/standard/tests/math/round_valid_rounding_mode.phpt
Normal file
@@ -0,0 +1,12 @@
|
||||
--TEST--
|
||||
round() rejects invalid rounding modes.
|
||||
--FILE--
|
||||
<?php
|
||||
try {
|
||||
var_dump(round(1.5, mode: 1234));
|
||||
} catch (ValueError $e) {
|
||||
echo $e->getMessage(), PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
round(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*)
|
||||
Reference in New Issue
Block a user