@@ -798,6 +798,12 @@ static zend_object_handlers bcmath_number_obj_handlers;
798
798
static zend_result bcmath_number_do_operation (uint8_t opcode , zval * ret_val , zval * op1 , zval * op2 );
799
799
static int bcmath_number_compare (zval * op1 , zval * op2 );
800
800
801
+ #if SIZEOF_SIZE_T >= 8
802
+ # define CHECK_RET_SCALE_OVERFLOW (scale , origin_scale ) (scale > INT_MAX)
803
+ #else
804
+ # define CHECK_RET_SCALE_OVERFLOW (scale , origin_scale ) (scale > INT_MAX || scale < origin_scale)
805
+ #endif
806
+
801
807
static zend_always_inline bcmath_number_obj_t * get_bcmath_number_from_obj (const zend_object * obj )
802
808
{
803
809
return (bcmath_number_obj_t * )((char * )(obj ) - XtOffsetOf (bcmath_number_obj_t , std ));
@@ -984,7 +990,7 @@ static zend_always_inline zend_result bcmath_number_mul_internal(
984
990
) {
985
991
if (auto_scale ) {
986
992
* scale = n1_full_scale + n2_full_scale ;
987
- if (UNEXPECTED (* scale > INT_MAX )) {
993
+ if (UNEXPECTED (CHECK_RET_SCALE_OVERFLOW ( * scale , n1_full_scale ) )) {
988
994
zend_value_error ("scale of the result is too large" );
989
995
return FAILURE ;
990
996
}
@@ -1001,7 +1007,7 @@ static zend_always_inline zend_result bcmath_number_div_internal(
1001
1007
) {
1002
1008
if (auto_scale ) {
1003
1009
* scale = n1_full_scale + BC_MATH_NUMBER_EXPAND_SCALE ;
1004
- if (UNEXPECTED (* scale > INT_MAX )) {
1010
+ if (UNEXPECTED (CHECK_RET_SCALE_OVERFLOW ( * scale , n1_full_scale ) )) {
1005
1011
zend_value_error ("scale of the result is too large" );
1006
1012
return FAILURE ;
1007
1013
}
@@ -1058,7 +1064,7 @@ static zend_always_inline zend_result bcmath_number_pow_internal(
1058
1064
}
1059
1065
} else if (exponent < 0 ) {
1060
1066
* scale = n1_full_scale + BC_MATH_NUMBER_EXPAND_SCALE ;
1061
- if (UNEXPECTED (* scale > INT_MAX )) {
1067
+ if (UNEXPECTED (CHECK_RET_SCALE_OVERFLOW ( * scale , n1_full_scale ) )) {
1062
1068
zend_value_error ("scale of the result is too large" );
1063
1069
return FAILURE ;
1064
1070
}
@@ -1574,7 +1580,7 @@ PHP_METHOD(BcMath_Number, sqrt)
1574
1580
size_t scale ;
1575
1581
if (scale_is_null ) {
1576
1582
scale = intern -> scale + BC_MATH_NUMBER_EXPAND_SCALE ;
1577
- if (UNEXPECTED (scale > INT_MAX )) {
1583
+ if (UNEXPECTED (CHECK_RET_SCALE_OVERFLOW ( scale , intern -> scale ) )) {
1578
1584
zend_value_error ("scale of the result is too large" );
1579
1585
RETURN_THROWS ();
1580
1586
}
0 commit comments