Skip to content

Commit 5fbb098

Browse files
committed
Merge branch 'PHP-7.1' into PHP-7.2
* PHP-7.1: Fix bug #76390 - do not allow invalid strings in range()
2 parents d5ee654 + 73bf238 commit 5fbb098

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

ext/standard/array.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,10 +2769,18 @@ PHP_FUNCTION(range)
27692769
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
27702770

27712771
if (zstep) {
2772-
if (Z_TYPE_P(zstep) == IS_DOUBLE ||
2773-
(Z_TYPE_P(zstep) == IS_STRING && is_numeric_string(Z_STRVAL_P(zstep), Z_STRLEN_P(zstep), NULL, NULL, 0) == IS_DOUBLE)
2774-
) {
2772+
if (Z_TYPE_P(zstep) == IS_DOUBLE) {
27752773
is_step_double = 1;
2774+
} else if (Z_TYPE_P(zstep) == IS_STRING) {
2775+
int type = is_numeric_string(Z_STRVAL_P(zstep), Z_STRLEN_P(zstep), NULL, NULL, 0);
2776+
if (type == IS_DOUBLE) {
2777+
is_step_double = 1;
2778+
}
2779+
if (type == 0) {
2780+
/* bad number */
2781+
php_error_docref(NULL, E_WARNING, "Invalid range string - must be numeric");
2782+
RETURN_FALSE;
2783+
}
27762784
}
27772785

27782786
step = zval_get_double(zstep);
@@ -2900,6 +2908,10 @@ PHP_FUNCTION(range)
29002908
}
29012909

29022910
lstep = step;
2911+
if (step <= 0) {
2912+
err = 1;
2913+
goto err;
2914+
}
29032915

29042916
Z_TYPE_INFO(tmp) = IS_LONG;
29052917
if (low > high) { /* Negative steps */

ext/standard/tests/array/range_errors.phpt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var_dump( range(1) ); // No.of args < expected
2727
var_dump( range(1,2,3,4) ); // No.of args > expected
2828
var_dump( range(-1, -2, 2) );
2929
var_dump( range("a", "j", "z") );
30+
var_dump( range(0, 1, "140962482048819216326.24") );
31+
var_dump( range(0, 1, "140962482048819216326.24.") );
3032

3133
echo "\n-- Testing Invalid steps --";
3234
$step_arr = array( "string", NULL, FALSE, "", "\0" );
@@ -78,11 +80,17 @@ bool(false)
7880
Warning: range(): step exceeds the specified range in %s on line %d
7981
bool(false)
8082

83+
Warning: range(): Invalid range string - must be numeric in %s on line %d
84+
bool(false)
85+
8186
Warning: range(): step exceeds the specified range in %s on line %d
8287
bool(false)
8388

89+
Warning: range(): Invalid range string - must be numeric in %s on line %d
90+
bool(false)
91+
8492
-- Testing Invalid steps --
85-
Warning: range(): step exceeds the specified range in %s on line %d
93+
Warning: range(): Invalid range string - must be numeric in %s on line %d
8694
bool(false)
8795

8896
Warning: range(): step exceeds the specified range in %s on line %d
@@ -91,9 +99,9 @@ bool(false)
9199
Warning: range(): step exceeds the specified range in %s on line %d
92100
bool(false)
93101

94-
Warning: range(): step exceeds the specified range in %s on line %d
102+
Warning: range(): Invalid range string - must be numeric in %s on line %d
95103
bool(false)
96104

97-
Warning: range(): step exceeds the specified range in %s on line %d
105+
Warning: range(): Invalid range string - must be numeric in %s on line %d
98106
bool(false)
99107
Done

0 commit comments

Comments
 (0)