Skip to content

Integer overflow with large numbers in LimitIterator #18421

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
chongwick opened this issue Apr 24, 2025 · 0 comments
Closed

Integer overflow with large numbers in LimitIterator #18421

chongwick opened this issue Apr 24, 2025 · 0 comments

Comments

@chongwick
Copy link

Description

The following code:

<?php

$a = array('zero' => 0, 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5);
//foreach (new ArrayIterator($a) as $k => $v)
foreach (new LimitIterator(new ArrayIterator($a), PHP_INT_MAX, PHP_INT_MAX) as $k => $v)
{
    var_dump(array($k, $v));
}

Resulted in this output:

/nightly_php/php-src/ext/spl/spl_iterators.c:2137:36: runtime error: signed integer overflow: 9223372036854775807 + 9223372036854775807 cannot be represented in type 'long int'

But I expected this output instead:

PHP Version

nightly

Operating System

ubuntu 22.04

@nielsdos nielsdos self-assigned this Apr 24, 2025
@nielsdos nielsdos changed the title int overflow spl Integer overflow with large numbers in LimitIterator Apr 24, 2025
nielsdos added a commit to nielsdos/php-src that referenced this issue Apr 24, 2025
Since we already know that `pos < intern->u.limit.offset` at this point,
we can reverse the expression.
nielsdos added a commit that referenced this issue Apr 25, 2025
* PHP-8.3:
  Fix GH-18421: Integer overflow with large numbers in LimitIterator
nielsdos added a commit that referenced this issue Apr 25, 2025
* PHP-8.4:
  Fix GH-18421: Integer overflow with large numbers in LimitIterator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants