Skip to content

Commit 7dbdb22

Browse files
committed
round(): Validate the rounding mode
1 parent 37ce719 commit 7dbdb22

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

ext/standard/math.c

Lines changed: 11 additions & 0 deletions
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. */
Lines changed: 12 additions & 0 deletions
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+
var_dump(round(1.5, mode: 1234));
6+
?>
7+
--EXPECTF--
8+
Fatal error: Uncaught ValueError: round(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*) in %s:%d
9+
Stack trace:
10+
#0 %s(%d): round(1.5, 0, 1234)
11+
#1 {main}
12+
thrown in %s on line %d

0 commit comments

Comments
 (0)