Skip to content

Commit f90b877

Browse files
Refactor ZEND_LONG_MAX/MIN checks into ZEND_DOUBLE_FITS_LONG()
1 parent d19ce51 commit f90b877

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

Zend/zend_API.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, cons
407407
if (zend_isnan(d)) {
408408
return "long";
409409
}
410-
if (d > ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
410+
if (!ZEND_DOUBLE_FITS_LONG(d)) {
411411
if (c == 'L') {
412412
*p = (d > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
413413
} else {
@@ -425,7 +425,7 @@ static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, cons
425425
if (zend_isnan(Z_DVAL_P(arg))) {
426426
return "long";
427427
}
428-
if (Z_DVAL_P(arg) > ZEND_LONG_MAX || Z_DVAL_P(arg) < ZEND_LONG_MIN) {
428+
if (!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg))) {
429429
if (c == 'L') {
430430
*p = (Z_DVAL_P(arg) > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
431431
} else {

Zend/zend_API.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ static zend_always_inline int _z_param_long(zval *arg, zend_long *dest, zend_boo
10671067
if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
10681068
return 0;
10691069
}
1070-
if (UNEXPECTED(Z_DVAL_P(arg) > ZEND_LONG_MAX || Z_DVAL_P(arg) < ZEND_LONG_MIN)) {
1070+
if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
10711071
/* Ironically, the strict parameter makes zpp *non*-strict here */
10721072
if (strict) {
10731073
*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
10861086
if (UNEXPECTED(zend_isnan(d))) {
10871087
return 0;
10881088
}
1089-
if (UNEXPECTED(d > ZEND_LONG_MAX || d < ZEND_LONG_MIN)) {
1089+
if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
10901090
if (strict) {
10911091
*dest = (d > 0) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
10921092
} else {

Zend/zend_operators.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ ZEND_API zend_uchar _is_numeric_string_ex(const char *str, size_t length, zend_l
8989

9090
END_EXTERN_C()
9191

92+
#if SIZEOF_ZEND_LONG == 4
93+
# define ZEND_DOUBLE_FITS_LONG(d) (!((d) > ZEND_LONG_MAX || (d) < ZEND_LONG_MIN))
94+
#else
95+
/* >= as (double)ZEND_LONG_MAX is outside signed range */
96+
# define ZEND_DOUBLE_FITS_LONG(d) (!((d) >= ZEND_LONG_MAX || (d) < ZEND_LONG_MIN))
97+
#endif
98+
9299
#if ZEND_DVAL_TO_LVAL_CAST_OK
93100
static zend_always_inline zend_long zend_dval_to_lval(double d)
94101
{
@@ -103,7 +110,7 @@ static zend_always_inline zend_long zend_dval_to_lval(double d)
103110
{
104111
if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
105112
return 0;
106-
} else if (d > ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
113+
} else if (!ZEND_DOUBLE_FITS_LONG(d)) {
107114
double two_pow_32 = pow(2., 32.),
108115
dmod;
109116

@@ -122,8 +129,7 @@ static zend_always_inline zend_long zend_dval_to_lval(double d)
122129
{
123130
if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
124131
return 0;
125-
/* >= as (double)ZEND_LONG_MAX is outside signed range */
126-
} else if (d >= ZEND_LONG_MAX || d < ZEND_LONG_MIN) {
132+
} else if (!ZEND_DOUBLE_FITS_LONG(d)) {
127133
double two_pow_64 = pow(2., 64.),
128134
dmod;
129135

ext/standard/tests/strings/chunk_split_variation2.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ Warning: chunk_split(): Chunk length should be greater than zero in %schunk_spli
100100
bool(false)
101101
-- Iteration 3 --
102102

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

107107
Warning: chunk_split(): Chunk length should be greater than zero in %schunk_split_variation2.php on line %d

0 commit comments

Comments
 (0)