Skip to content

Commit a91d913

Browse files
committed
Fix phpGH-18421: Integer overflow with large numbers in LimitIterator
Since we already know that `pos < intern->u.limit.offset` at this point, we can reverse the expression. Closes phpGH-18424.
1 parent 32e0912 commit a91d913

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ PHP NEWS
77
inaccurate sunrise and sunset times, but other calculated times are
88
correct) (JiriJozif).
99

10+
- SPL:
11+
. Fixed bug GH-18421 (Integer overflow with large numbers in LimitIterator).
12+
(nielsdos)
13+
1014
- Standard:
1115
. Fixed bug GH-17403 (Potential deadlock when putenv fails). (nielsdos)
1216

ext/spl/spl_iterators.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ static inline void spl_limit_it_seek(spl_dual_it_object *intern, zend_long pos)
22282228
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Cannot seek to " ZEND_LONG_FMT " which is below the offset " ZEND_LONG_FMT, pos, intern->u.limit.offset);
22292229
return;
22302230
}
2231-
if (pos >= intern->u.limit.offset + intern->u.limit.count && intern->u.limit.count != -1) {
2231+
if (pos - intern->u.limit.offset >= intern->u.limit.count && intern->u.limit.count != -1) {
22322232
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Cannot seek to " ZEND_LONG_FMT " which is behind offset " ZEND_LONG_FMT " plus count " ZEND_LONG_FMT, pos, intern->u.limit.offset, intern->u.limit.count);
22332233
return;
22342234
}

ext/spl/tests/gh18421.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-18421 (Integer overflow with large numbers in LimitIterator)
3+
--FILE--
4+
<?php
5+
6+
$a = array('zero' => 0, 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5);
7+
try {
8+
foreach (new LimitIterator(new ArrayIterator($a), PHP_INT_MAX, PHP_INT_MAX) as $k => $v)
9+
{
10+
}
11+
} catch (OutOfBoundsException $e) {
12+
echo $e->getMessage(), "\n";
13+
}
14+
15+
?>
16+
--EXPECTF--
17+
Seek position %d is out of range

0 commit comments

Comments
 (0)