From: Marcus Boerger Date: Sat, 5 Mar 2005 11:35:13 +0000 (+0000) Subject: - Negative seeking is not supported X-Git-Tag: RELEASE_0_3~76 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e4ce3e36efd5d2b9e9be66767db06c14d690b32a;p=php - Negative seeking is not supported --- diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index 20ceb691ae..c35f583aec 100755 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -813,16 +813,19 @@ SPL_METHOD(Array, seek) return; } - zend_hash_internal_pointer_reset_ex(aht, &intern->pos); - opos = position; - while (position-- > 0 && (result = spl_array_next(intern TSRMLS_CC)) == SUCCESS); - if (intern->pos && intern->is_ref && spl_hash_verify_pos(intern TSRMLS_CC) == FAILURE) { - /* fail */ - } else { - if (zend_hash_has_more_elements_ex(aht, &intern->pos) == SUCCESS) { - return; /* ok */ + if (position >= 0) { /* negative values are not supported */ + zend_hash_internal_pointer_reset_ex(aht, &intern->pos); + + while (position-- > 0 && (result = spl_array_next(intern TSRMLS_CC)) == SUCCESS); + + if (intern->pos && intern->is_ref && spl_hash_verify_pos(intern TSRMLS_CC) == FAILURE) { + /* fail */ + } else { + if (zend_hash_has_more_elements_ex(aht, &intern->pos) == SUCCESS) { + return; /* ok */ + } } } zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Seek position %ld is out of range", opos); diff --git a/ext/spl/tests/array_014.phpt b/ext/spl/tests/array_014.phpt index 8accd97dfc..ad9bc6c4ac 100755 --- a/ext/spl/tests/array_014.phpt +++ b/ext/spl/tests/array_014.phpt @@ -11,8 +11,16 @@ $it->seek(5); var_dump($it->current()); $it->seek(4); var_dump($it->current()); -$it->seek(-1); -var_dump($it->current()); +try +{ + $it->seek(-1); + var_dump($it->current()); +} +catch(Exception $e) +{ + echo $e->getMessage() . "\n"; +} + try { $it->seek(12); @@ -37,7 +45,7 @@ foreach($it as $v) int(11) int(5) int(4) -int(0) +Seek position -1 is out of range Seek position 12 is out of range int(0) int(1)