Skip to content

round(): Validate the rounding mode #12252

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

Merged
merged 4 commits into from
Sep 22, 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 NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,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! >>>
3 changes: 3 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Comment on lines +338 to +348
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously an invalid rounding mode was considered to be equal to PHP_ROUND_HALF_UP. This is no longer the case since #12220. Making this a ValueError would be correct, but possibly a little steep. Shall I update this to a E_WARNING + change mode to PHP_ROUND_HALF_UP within the switch()? I don't want to update the rounding helper to handle unknown cases to keep the function clean.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having it directly a ValueError is fine IMHO, just add an entry in UPGRADING

switch (Z_TYPE_P(value)) {
case IS_LONG:
/* Simple case - long that doesn't need to be rounded. */
Expand Down
12 changes: 12 additions & 0 deletions ext/standard/tests/math/round_valid_rounding_mode.phpt
Original file line number Diff line number Diff line change
@@ -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_*)