Skip to content

Commit c567016

Browse files
committed
Detect overlarge step for character range()
This was done for int and float ranges, but not char ranges. Fixes oss-fuzz #28666.
1 parent 205d209 commit c567016

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

ext/standard/array.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,7 @@ PHP_FUNCTION(range)
27562756
high = (unsigned char)Z_STRVAL_P(zhigh)[0];
27572757

27582758
if (low > high) { /* Negative Steps */
2759-
if (lstep <= 0) {
2759+
if (low - high < lstep || lstep <= 0) {
27602760
err = 1;
27612761
goto err;
27622762
}
@@ -2773,7 +2773,7 @@ PHP_FUNCTION(range)
27732773
}
27742774
} ZEND_HASH_FILL_END();
27752775
} else if (high > low) { /* Positive Steps */
2776-
if (lstep <= 0) {
2776+
if (high - low < lstep || lstep <= 0) {
27772777
err = 1;
27782778
goto err;
27792779
}

ext/standard/tests/array/range_errors.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ try {
4747
echo $e->getMessage(), "\n";
4848
}
4949

50+
echo "\n\n-- Testing ( (low < high) && (high-low < step) ) for characters --\n";
51+
try {
52+
var_dump(range('a', 'z', 100));
53+
} catch (\ValueError $e) {
54+
echo $e->getMessage(), "\n";
55+
}
56+
57+
echo "\n\n-- Testing ( (low > high) && (low-high < step) ) for characters --\n";
58+
try {
59+
var_dump(range('z', 'a', 100));
60+
} catch (\ValueError $e) {
61+
echo $e->getMessage(), "\n";
62+
}
63+
5064
echo "\n-- Testing other conditions --\n";
5165
try {
5266
var_dump( range(-1, -2, 2) );
@@ -97,6 +111,14 @@ range(): Argument #3 ($step) must not exceed the specified range
97111
-- Testing ( (low > high) && (low-high < step) ) --
98112
range(): Argument #3 ($step) must not exceed the specified range
99113

114+
115+
-- Testing ( (low < high) && (high-low < step) ) for characters --
116+
range(): Argument #3 ($step) must not exceed the specified range
117+
118+
119+
-- Testing ( (low > high) && (low-high < step) ) for characters --
120+
range(): Argument #3 ($step) must not exceed the specified range
121+
100122
-- Testing other conditions --
101123
range(): Argument #3 ($step) must not exceed the specified range
102124
range(): Argument #3 ($step) must be of type int|float, string given

0 commit comments

Comments
 (0)