Refactor ZEND_LONG_MAX/MIN checks into ZEND_DOUBLE_FITS_LONG()

This commit is contained in:
Andrea Faulds 2014-11-29 02:13:20 +00:00
parent d19ce51854
commit f90b877f41
4 changed files with 15 additions and 9 deletions

View File

@ -407,7 +407,7 @@ static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, cons
if (zend_isnan(d)) {
return "long";
}
if (d > ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
if (!ZEND_DOUBLE_FITS_LONG(d)) {
if (c == 'L') {
*p = (d > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
} else {
@ -425,7 +425,7 @@ static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, cons
if (zend_isnan(Z_DVAL_P(arg))) {
return "long";
}
if (Z_DVAL_P(arg) > ZEND_LONG_MAX || Z_DVAL_P(arg) < ZEND_LONG_MIN) {
if (!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg))) {
if (c == 'L') {
*p = (Z_DVAL_P(arg) > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
} else {

View File

@ -1067,7 +1067,7 @@ static zend_always_inline int _z_param_long(zval *arg, zend_long *dest, zend_boo
if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
return 0;
}
if (UNEXPECTED(Z_DVAL_P(arg) > ZEND_LONG_MAX || Z_DVAL_P(arg) < ZEND_LONG_MIN)) {
if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
/* Ironically, the strict parameter makes zpp *non*-strict here */
if (strict) {
*dest = (Z_DVAL_P(arg) > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
@ -1086,7 +1086,7 @@ static zend_always_inline int _z_param_long(zval *arg, zend_long *dest, zend_boo
if (UNEXPECTED(zend_isnan(d))) {
return 0;
}
if (UNEXPECTED(d > ZEND_LONG_MAX || d < ZEND_LONG_MIN)) {
if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
if (strict) {
*dest = (d > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
} else {

View File

@ -89,6 +89,13 @@ ZEND_API zend_uchar _is_numeric_string_ex(const char *str, size_t length, zend_l
END_EXTERN_C()
#if SIZEOF_ZEND_LONG == 4
# define ZEND_DOUBLE_FITS_LONG(d) (!((d) > ZEND_LONG_MAX || (d) < ZEND_LONG_MIN))
#else
/* >= as (double)ZEND_LONG_MAX is outside signed range */
# define ZEND_DOUBLE_FITS_LONG(d) (!((d) >= ZEND_LONG_MAX || (d) < ZEND_LONG_MIN))
#endif
#if ZEND_DVAL_TO_LVAL_CAST_OK
static zend_always_inline zend_long zend_dval_to_lval(double d)
{
@ -103,7 +110,7 @@ static zend_always_inline zend_long zend_dval_to_lval(double d)
{
if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
return 0;
} else if (d > ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
} else if (!ZEND_DOUBLE_FITS_LONG(d)) {
double two_pow_32 = pow(2., 32.),
dmod;
@ -122,8 +129,7 @@ static zend_always_inline zend_long zend_dval_to_lval(double d)
{
if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
return 0;
/* >= as (double)ZEND_LONG_MAX is outside signed range */
} else if (d >= ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
} else if (!ZEND_DOUBLE_FITS_LONG(d)) {
double two_pow_64 = pow(2., 64.),
dmod;

View File

@ -100,8 +100,8 @@ Warning: chunk_split(): Chunk length should be greater than zero in %schunk_spli
bool(false)
-- Iteration 3 --
Warning: chunk_split(): Chunk length should be greater than zero in %schunk_split_variation2.php on line %d
bool(false)
Warning: chunk_split() expects parameter 2 to be long, double given in %s on line %d
NULL
-- Iteration 4 --
Warning: chunk_split(): Chunk length should be greater than zero in %schunk_split_variation2.php on line %d