Skip to content

Commit 15bea9e

Browse files
committed
Fix GH-14775: range overflow on negative step.
overflow occurs since we only deal with positive steps. close GH-14778
1 parent 6b54d3b commit 15bea9e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

NEWS

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ PHP NEWS
2929
(David Carlier)
3030

3131
- Shmop:
32-
. Fixed bug GH-14537 (shmop Windows 11 crashes the process). (nielsdos)
32+
. Fixed bug GH-14537 (shmop Windows 11 crashes the process). (nielsdos)
33+
34+
- Standard:
35+
. Fixed bug GH-14775 (range function overflow with negative step argument).
36+
(David Carlier)
3337

3438
20 Jun 2024, PHP 8.3.9
3539

ext/standard/array.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,10 @@ PHP_FUNCTION(range)
28872887
step = Z_LVAL_P(user_step);
28882888
/* We only want positive step values. */
28892889
if (step < 0) {
2890+
if (UNEXPECTED(step == ZEND_LONG_MIN)) {
2891+
zend_argument_value_error(3, "must be greater than " ZEND_LONG_FMT, step);
2892+
RETURN_THROWS();
2893+
}
28902894
is_step_negative = true;
28912895
step *= -1;
28922896
}

ext/standard/tests/array/gh14775.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-14775: Range negative step overflow
3+
--FILE--
4+
<?php
5+
$var = -PHP_INT_MAX - 1;
6+
try {
7+
range($var,1,$var);
8+
} catch (\ValueError $e) {
9+
echo $e->getMessage() . PHP_EOL;
10+
}
11+
--EXPECTF--
12+
range(): Argument #3 ($step) must be greater than %s

0 commit comments

Comments
 (0)