From 7dbdb22eeffefb91da4762e985bee9232e4d062b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 20 Sep 2023 19:06:16 +0200 Subject: [PATCH 1/4] round(): Validate the rounding mode --- ext/standard/math.c | 11 +++++++++++ .../tests/math/round_valid_rounding_mode.phpt | 12 ++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 ext/standard/tests/math/round_valid_rounding_mode.phpt diff --git a/ext/standard/math.c b/ext/standard/math.c index 05f2252e304e8..6b16b0755f69b 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -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. */ diff --git a/ext/standard/tests/math/round_valid_rounding_mode.phpt b/ext/standard/tests/math/round_valid_rounding_mode.phpt new file mode 100644 index 0000000000000..9c59cfd111d15 --- /dev/null +++ b/ext/standard/tests/math/round_valid_rounding_mode.phpt @@ -0,0 +1,12 @@ +--TEST-- +round() rejects invalid rounding modes. +--FILE-- + +--EXPECTF-- +Fatal error: Uncaught ValueError: round(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*) in %s:%d +Stack trace: +#0 %s(%d): round(1.5, 0, 1234) +#1 {main} + thrown in %s on line %d From fda8abbe98580b4f99492179717ddab6e7376b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 22 Sep 2023 17:28:03 +0200 Subject: [PATCH 2/4] Avoid EXPECTF in round_valid_rounding_mode.phpt --- .../tests/math/round_valid_rounding_mode.phpt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ext/standard/tests/math/round_valid_rounding_mode.phpt b/ext/standard/tests/math/round_valid_rounding_mode.phpt index 9c59cfd111d15..d61bc6356e1e6 100644 --- a/ext/standard/tests/math/round_valid_rounding_mode.phpt +++ b/ext/standard/tests/math/round_valid_rounding_mode.phpt @@ -2,11 +2,11 @@ round() rejects invalid rounding modes. --FILE-- getMessage(), PHP_EOL; +} ?> ---EXPECTF-- -Fatal error: Uncaught ValueError: round(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*) in %s:%d -Stack trace: -#0 %s(%d): round(1.5, 0, 1234) -#1 {main} - thrown in %s on line %d +--EXPECT-- +round(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*) From b5a00d0275983ac5bce9c5ff3d11e35cde4b73f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 22 Sep 2023 17:30:00 +0200 Subject: [PATCH 3/4] [ci skip] UPGRADING --- UPGRADING | 3 +++ 1 file changed, 3 insertions(+) diff --git a/UPGRADING b/UPGRADING index 1dfb256097d2c..e354de6043e51 100644 --- a/UPGRADING +++ b/UPGRADING @@ -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 From 12f05bce69874329b927a7f73373e89e5cbea604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 22 Sep 2023 17:31:34 +0200 Subject: [PATCH 4/4] [ci skip] NEWS --- NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS b/NEWS index ce47d11ef0ab6..9776998e35eec 100644 --- a/NEWS +++ b/NEWS @@ -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! >>>