Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 20c9309

Browse files
committedApr 28, 2025··
Error handling is separated as a static function
1 parent 91e3890 commit 20c9309

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed
 

‎ext/bcmath/bcmath.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,26 @@ static zend_result php_str2num(bc_num *num, const zend_string *str)
179179
}
180180
/* }}} */
181181

182+
static void bc_pow_err(raise_mod_status status, uint32_t arg_num)
183+
{
184+
/* If arg_num is 0, it means it is an op */
185+
switch (status) {
186+
case BC_RAISE_STATUS_DIVIDE_BY_ZERO:
187+
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Negative power of zero");
188+
break;
189+
case BC_RAISE_STATUS_LEN_IS_OVERFLOW:
190+
case BC_RAISE_STATUS_SCALE_IS_OVERFLOW:
191+
case BC_RAISE_STATUS_FULLLEN_IS_OVERFLOW:
192+
if (arg_num == 0) {
193+
zend_value_error("exponent is too large, the number of digits overflowed");
194+
} else {
195+
zend_argument_value_error(arg_num, "exponent is too large, the number of digits overflowed");
196+
}
197+
break;
198+
EMPTY_SWITCH_DEFAULT_CASE();
199+
}
200+
}
201+
182202
/* {{{ Returns the sum of two arbitrary precision numbers */
183203
PHP_FUNCTION(bcadd)
184204
{
@@ -615,18 +635,10 @@ PHP_FUNCTION(bcpow)
615635
goto cleanup;
616636
}
617637

618-
switch (bc_raise(first, exponent, &result, scale)) {
619-
case BC_RAISE_STATUS_OK:
620-
break;
621-
case BC_RAISE_STATUS_DIVIDE_BY_ZERO:
622-
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Negative power of zero");
623-
goto cleanup;
624-
case BC_RAISE_STATUS_LEN_IS_OVERFLOW:
625-
case BC_RAISE_STATUS_SCALE_IS_OVERFLOW:
626-
case BC_RAISE_STATUS_FULLLEN_IS_OVERFLOW:
627-
zend_argument_value_error(2, "exponent is too large, the number of digits overflowed");
628-
goto cleanup;
629-
EMPTY_SWITCH_DEFAULT_CASE();
638+
bc_raise_status ret_status = bc_raise(first, exponent, &result, scale);
639+
if (UNEXPECTED(ret_status != BC_RAISE_STATUS_OK)) {
640+
bc_pow_err(ret_status, 2);
641+
goto cleanup;
630642
}
631643
RETVAL_NEW_STR(bc_num2str_ex(result, scale));
632644

@@ -1152,22 +1164,10 @@ static zend_result bcmath_number_pow_internal(
11521164
}
11531165
return FAILURE;
11541166
}
1155-
switch (bc_raise(n1, exponent, ret, *scale)) {
1156-
case BC_RAISE_STATUS_OK:
1157-
break;
1158-
case BC_RAISE_STATUS_DIVIDE_BY_ZERO:
1159-
zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Negative power of zero");
1160-
return FAILURE;
1161-
case BC_RAISE_STATUS_LEN_IS_OVERFLOW:
1162-
case BC_RAISE_STATUS_SCALE_IS_OVERFLOW:
1163-
case BC_RAISE_STATUS_FULLLEN_IS_OVERFLOW:
1164-
if (is_op) {
1165-
zend_value_error("exponent is too large, the number of digits overflowed");
1166-
} else {
1167-
zend_argument_value_error(1, "exponent is too large, the number of digits overflowed");
1168-
}
1169-
return FAILURE;
1170-
EMPTY_SWITCH_DEFAULT_CASE();
1167+
bc_raise_status ret_status = bc_raise(n1, exponent, ret, *scale);
1168+
if (UNEXPECTED(ret_status != BC_RAISE_STATUS_OK)) {
1169+
bc_pow_err(ret_status, is_op ? 0 : 1);
1170+
return FAILURE;
11711171
}
11721172
bc_rm_trailing_zeros(*ret);
11731173
if (scale_expand) {

0 commit comments

Comments
 (0)
Please sign in to comment.