Skip to content

Commit 5cbd2c0

Browse files
committed
Review feedback by Saki
1 parent da80c86 commit 5cbd2c0

3 files changed

Lines changed: 15 additions & 24 deletions

File tree

ext/bcmath/bcmath.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ static zend_always_inline zend_result bcmath_check_scale(zend_long scale, uint32
157157

158158
static zend_result bcmath_check_precision(zend_long precision, uint32_t arg_num)
159159
{
160-
if (ZEND_LONG_EXCEEDS_INT(precision)) {
161-
zend_argument_value_error(arg_num, "must be between %d and %d", INT_MIN, INT_MAX);
160+
if (ZEND_LONG_INT_OVFL(precision)) {
161+
zend_argument_value_error(arg_num, "must be between " ZEND_LONG_FMT " and %d",
162+
(zend_long) ZEND_LONG_MIN, INT_MAX);
162163
return FAILURE;
163164
}
164165
return SUCCESS;

ext/bcmath/libbcmath/src/round.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ size_t bc_round(bc_num num, zend_long precision, zend_enum_RoundingMode mode, bc
6767
return 0;
6868
}
6969

70-
/* If precision is -3, it becomes 1000. */
71-
if (UNEXPECTED(precision == ZEND_LONG_MIN)) {
72-
*result = bc_new_num((size_t) ZEND_LONG_MAX + 2, 0);
73-
} else {
74-
*result = bc_new_num(-precision + 1, 0);
75-
}
70+
/* If precision is -3, it becomes 1000. Negate in unsigned space so
71+
* precision == ZEND_LONG_MIN doesn't overflow signed long. */
72+
zend_ulong magnitude = -(zend_ulong) precision;
73+
*result = bc_new_num((size_t) magnitude + 1, 0);
7674
(*result)->n_value[0] = 1;
7775
(*result)->n_sign = num->n_sign;
7876
return 0;
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
bcround() and BcMath\Number::round() reject out-of-range $precision
2+
bcround() and BcMath\Number::round() reject $precision above INT_MAX
33
--EXTENSIONS--
44
bcmath
55
--SKIPIF--
@@ -11,16 +11,6 @@ try {
1111
} catch (\ValueError $e) {
1212
echo $e->getMessage() . \PHP_EOL;
1313
}
14-
try {
15-
bcround('12345', -PHP_INT_MAX, RoundingMode::AwayFromZero);
16-
} catch (\ValueError $e) {
17-
echo $e->getMessage() . \PHP_EOL;
18-
}
19-
try {
20-
bcround('12345', PHP_INT_MIN, RoundingMode::AwayFromZero);
21-
} catch (\ValueError $e) {
22-
echo $e->getMessage() . \PHP_EOL;
23-
}
2414
try {
2515
(new BcMath\Number('1'))->round(PHP_INT_MAX);
2616
} catch (\ValueError $e) {
@@ -31,10 +21,12 @@ try {
3121
} catch (\ValueError $e) {
3222
echo $e->getMessage() . \PHP_EOL;
3323
}
24+
25+
// Large negative precision is accepted: result is a power of 10.
26+
echo bcround('1', -5, RoundingMode::AwayFromZero) . \PHP_EOL;
3427
?>
3528
--EXPECTF--
36-
bcround(): Argument #2 ($precision) must be between %i and %i
37-
bcround(): Argument #2 ($precision) must be between %i and %i
38-
bcround(): Argument #2 ($precision) must be between %i and %i
39-
BcMath\Number::round(): Argument #1 ($precision) must be between %i and %i
40-
BcMath\Number::round(): Argument #1 ($precision) must be between %i and %i
29+
bcround(): Argument #2 ($precision) must be between %i and %d
30+
BcMath\Number::round(): Argument #1 ($precision) must be between %i and %d
31+
BcMath\Number::round(): Argument #1 ($precision) must be between %i and %d
32+
100000

0 commit comments

Comments
 (0)