Skip to content

Fix GH-14775: range overflow on negative step. #14778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2887,6 +2887,10 @@ PHP_FUNCTION(range)
step = Z_LVAL_P(user_step);
/* We only want positive step values. */
if (step < 0) {
if (UNEXPECTED(step == ZEND_LONG_MIN)) {
zend_argument_value_error(3, "must be greater than " ZEND_LONG_FMT, step);
RETURN_THROWS();
}
is_step_negative = true;
step *= -1;
}
Expand Down
12 changes: 12 additions & 0 deletions ext/standard/tests/array/gh14775.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
GH-14775: Range negative step overflow
--FILE--
<?php
$var = -PHP_INT_MAX - 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not PHP_INT_MIN?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not ? it s just a test case, relax :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO PHP_INT_MIN is much readable, isn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this is way less readable.

try {
range($var,1,$var);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
--EXPECTF--
range(): Argument #3 ($step) must be greater than %s
Loading