Skip to content

Commit

Permalink
Detect overlarge step for character range()
Browse files Browse the repository at this point in the history
This was done for int and float ranges, but not char ranges.

Fixes oss-fuzz #28666.
  • Loading branch information
nikic committed Dec 16, 2020
1 parent 205d209 commit c567016
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2756,7 +2756,7 @@ PHP_FUNCTION(range)
high = (unsigned char)Z_STRVAL_P(zhigh)[0];

if (low > high) { /* Negative Steps */
if (lstep <= 0) {
if (low - high < lstep || lstep <= 0) {
err = 1;
goto err;
}
Expand All @@ -2773,7 +2773,7 @@ PHP_FUNCTION(range)
}
} ZEND_HASH_FILL_END();
} else if (high > low) { /* Positive Steps */
if (lstep <= 0) {
if (high - low < lstep || lstep <= 0) {
err = 1;
goto err;
}
Expand Down
22 changes: 22 additions & 0 deletions ext/standard/tests/array/range_errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ try {
echo $e->getMessage(), "\n";
}

echo "\n\n-- Testing ( (low < high) && (high-low < step) ) for characters --\n";
try {
var_dump(range('a', 'z', 100));
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}

echo "\n\n-- Testing ( (low > high) && (low-high < step) ) for characters --\n";
try {
var_dump(range('z', 'a', 100));
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}

echo "\n-- Testing other conditions --\n";
try {
var_dump( range(-1, -2, 2) );
Expand Down Expand Up @@ -97,6 +111,14 @@ range(): Argument #3 ($step) must not exceed the specified range
-- Testing ( (low > high) && (low-high < step) ) --
range(): Argument #3 ($step) must not exceed the specified range


-- Testing ( (low < high) && (high-low < step) ) for characters --
range(): Argument #3 ($step) must not exceed the specified range


-- Testing ( (low > high) && (low-high < step) ) for characters --
range(): Argument #3 ($step) must not exceed the specified range

-- Testing other conditions --
range(): Argument #3 ($step) must not exceed the specified range
range(): Argument #3 ($step) must be of type int|float, string given
Expand Down

0 comments on commit c567016

Please sign in to comment.