Skip to content

Commit fd3ef91

Browse files
Fix GH-19577: avoid integer overflow when using a small offset and PHP_INT_MAX with LimitIterator
1 parent 96c0bc5 commit fd3ef91

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ PHP NEWS
2323

2424
- Standard:
2525
. Fixed bug GH-16649 (UAF during array_splice). (alexandre-daubois)
26+
. Fixed bug GH-19577 (Avoid integer overflow when using a small offset
27+
and PHP_INT_MAX with LimitIterator). (alexandre-daubois)
2628

2729
- Tidy:
2830
. Fixed GH-19021 build issue with libtidy in regard of tidyOptIsReadonly

ext/spl/spl_iterators.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,14 +2209,27 @@ static zend_object *spl_dual_it_new(zend_class_entry *class_type)
22092209
}
22102210
/* }}} */
22112211

2212+
/* Returns the relative position for the current iterator position. */
2213+
static zend_long spl_limit_it_relative_pos(spl_dual_it_object *intern)
2214+
{
2215+
return intern->current.pos - intern->u.limit.offset;
2216+
}
2217+
2218+
/* Returns the relative position for an arbitrary position. */
2219+
static zend_long spl_limit_it_relative_pos_for(spl_dual_it_object *intern, zend_long pos)
2220+
{
2221+
return pos - intern->u.limit.offset;
2222+
}
2223+
22122224
static inline int spl_limit_it_valid(spl_dual_it_object *intern)
22132225
{
22142226
/* FAILURE / SUCCESS */
2215-
if (intern->u.limit.count != -1 && intern->current.pos >= intern->u.limit.offset + intern->u.limit.count) {
2227+
if (intern->u.limit.count != -1 &&
2228+
spl_limit_it_relative_pos(intern) >= intern->u.limit.count) {
22162229
return FAILURE;
2217-
} else {
2218-
return spl_dual_it_valid(intern);
22192230
}
2231+
2232+
return spl_dual_it_valid(intern);
22202233
}
22212234

22222235
static inline void spl_limit_it_seek(spl_dual_it_object *intern, zend_long pos)
@@ -2228,7 +2241,7 @@ static inline void spl_limit_it_seek(spl_dual_it_object *intern, zend_long pos)
22282241
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);
22292242
return;
22302243
}
2231-
if (pos - intern->u.limit.offset >= intern->u.limit.count && intern->u.limit.count != -1) {
2244+
if (spl_limit_it_relative_pos_for(intern, pos) >= intern->u.limit.count && intern->u.limit.count != -1) {
22322245
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);
22332246
return;
22342247
}
@@ -2289,7 +2302,7 @@ PHP_METHOD(LimitIterator, valid)
22892302
SPL_FETCH_AND_CHECK_DUAL_IT(intern, ZEND_THIS);
22902303

22912304
/* RETURN_BOOL(spl_limit_it_valid(intern) == SUCCESS);*/
2292-
RETURN_BOOL((intern->u.limit.count == -1 || intern->current.pos < intern->u.limit.offset + intern->u.limit.count) && Z_TYPE(intern->current.data) != IS_UNDEF);
2305+
RETURN_BOOL((intern->u.limit.count == -1 || spl_limit_it_relative_pos(intern) < intern->u.limit.count) && Z_TYPE(intern->current.data) != IS_UNDEF);
22932306
} /* }}} */
22942307

22952308
/* {{{ Move the iterator forward */
@@ -2304,7 +2317,7 @@ PHP_METHOD(LimitIterator, next)
23042317
SPL_FETCH_AND_CHECK_DUAL_IT(intern, ZEND_THIS);
23052318

23062319
spl_dual_it_next(intern, 1);
2307-
if (intern->u.limit.count == -1 || intern->current.pos < intern->u.limit.offset + intern->u.limit.count) {
2320+
if (intern->u.limit.count == -1 || spl_limit_it_relative_pos(intern) < intern->u.limit.count) {
23082321
spl_dual_it_fetch(intern, 1);
23092322
}
23102323
} /* }}} */

ext/spl/tests/gh19577.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
GH-19577: Integer overflow in LimitIterator with small offset and PHP_INT_MAX count
3+
--FILE--
4+
<?php
5+
6+
$it = new ArrayIterator(array(0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D'));
7+
$it = new LimitIterator($it, 2, PHP_INT_MAX);
8+
9+
foreach($it as $val => $key) {
10+
echo "Key: $val, Value: $key\n";
11+
}
12+
13+
?>
14+
--EXPECT--
15+
Key: 2, Value: C
16+
Key: 3, Value: D

0 commit comments

Comments
 (0)