Skip to content

Commit 54f798a

Browse files
committed
wip
1 parent a6a8184 commit 54f798a

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

ext/bcmath/bcmath.c

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -817,20 +817,26 @@ static zend_object *bc_num_clone_obj(zend_object *object)
817817
}
818818
/* }}} */
819819

820-
static zend_result convert_zval_to_bc_num(zval *zv, bc_num *num)
820+
static zend_result convert_zval_to_bc_num(zval *zv, bc_num *num, bool *should_free)
821821
{
822+
*should_free = false;
823+
822824
switch (Z_TYPE_P(zv)) {
823825
case IS_LONG:
824826
case IS_STRING:
825-
convert_to_string(zv);
826-
bc_init_num(&num1);
827-
if (!bc_str2num(num, Z_STRVAL_P(zv), 0)) {
828-
return FAILURE;
827+
{
828+
zend_string *str = zval_get_string(zv);
829+
bc_init_num(num);
830+
*should_free = true;
831+
if (!bc_str2num(num, ZSTR_VAL(str), 0)) {
832+
zend_string_release(str);
833+
return FAILURE;
834+
}
835+
zend_string_release(str);
829836
}
830837
break;
831838
case IS_OBJECT:
832839
if (instanceof_function(Z_OBJCE_P(zv), bc_num_ce)) {
833-
bc_free_num(num);
834840
*num = bc_num_obj_from_zval(zv)->bc_num;
835841
} else {
836842
zend_argument_type_error(0, "must be of type int, string, or BcNum, %s given", zend_zval_value_name(zv));
@@ -845,12 +851,14 @@ static zend_result convert_zval_to_bc_num(zval *zv, bc_num *num)
845851
static zend_result bc_num_calculation(zval *result, zval *op1, zval *op2, bc_num_calculation_type type, bool is_operator)
846852
{
847853
bc_num num1, num2;
854+
bc_num_obj *result_obj;
855+
bool should_free1, should_free2;
848856

849-
if (convert_zval_to_bc_num(op1, &num1) == FAILURE || convert_zval_to_bc_num(op2, &num2) == FAILURE) {
850-
return FAILURE;
857+
if (convert_zval_to_bc_num(op1, &num1, &should_free1) == FAILURE || convert_zval_to_bc_num(op2, &num2, &should_free2) == FAILURE) {
858+
goto cleanup;
851859
}
852860

853-
bc_num_obj *result_obj = bc_num_obj_from_obj(bc_num_create_obj(bc_num_ce));
861+
result_obj = bc_num_obj_from_obj(bc_num_create_obj(bc_num_ce));
854862
bc_init_num(&result_obj->bc_num);
855863

856864
size_t scale = MAX(num1->n_scale, num2->n_scale);
@@ -876,17 +884,34 @@ static zend_result bc_num_calculation(zval *result, zval *op1, zval *op2, bc_num
876884
long exponent = bc_num2long(num2);
877885
if (exponent == 0 && (num2->n_len > 1 || num2->n_value[0] != 0)) {
878886
zend_argument_value_error(is_operator ? 0 : 1, "exponent is too large");
879-
return FAILURE;
887+
goto cleanup;
880888
}
881889
bc_raise(num1, bc_num2long(num2), &result_obj->bc_num, scale);
882890
}
883891
break;
884892
EMPTY_SWITCH_DEFAULT_CASE()
885893
}
886894

895+
if (should_free1) {
896+
bc_free_num(&num1);
897+
}
898+
if (should_free2) {
899+
bc_free_num(&num2);
900+
}
901+
887902
result_obj->bc_num->n_scale = scale;
888903
ZVAL_OBJ(result, &result_obj->std);
889904
return SUCCESS;
905+
906+
cleanup:
907+
if (should_free1) {
908+
bc_free_num(&num1);
909+
}
910+
if (should_free2) {
911+
bc_free_num(&num2);
912+
}
913+
bc_free_num(&result_obj->bc_num);
914+
return FAILURE;
890915
}
891916

892917
/* {{{ bc_num_obj_handlers.do_operation */

0 commit comments

Comments
 (0)