Skip to content

Commit

Permalink
Throw from MultipleIterator::key/current() for invalid iterator
Browse files Browse the repository at this point in the history
Calling current()/key() on an invalid iterator is an error
condition. Throw instead of returning false.
  • Loading branch information
nikic committed Jul 14, 2021
1 parent 1bd779c commit 42cb5b5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
4 changes: 3 additions & 1 deletion ext/spl/spl_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,9 @@ static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_

num_elements = zend_hash_num_elements(&intern->storage);
if (num_elements < 1) {
RETURN_FALSE;
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Called %s() on an invalid iterator",
get_type == SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT ? "current" : "key");
RETURN_THROWS();
}

array_init_size(return_value, num_elements);
Expand Down
6 changes: 3 additions & 3 deletions ext/spl/spl_observer.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function valid() {}
/** @return int */
public function key() {}

/** @return object|null */
/** @return object */
public function current() {}

/** @return void */
Expand Down Expand Up @@ -141,10 +141,10 @@ public function rewind() {}
/** @return bool */
public function valid() {}

/** @return array|false */
/** @return array */
public function key() {}

/** @return array|false */
/** @return array */
public function current() {}

/** @return void */
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/spl_observer_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 89437500594aa41fa5cd36f31de07ad65f2bf408 */
* Stub hash: b82f26b4c1340b58c0faa31c5e14fef4c9778928 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SplObserver_update, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, subject, SplSubject, 0)
Expand Down
14 changes: 12 additions & 2 deletions ext/spl/tests/multiple_iterator_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ echo "-- Default flags, no iterators --\n";
foreach($m as $value) {
var_dump($value);
}
var_dump($m->current());
try {
var_dump($m->current());
} catch (RuntimeException $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($m->key());
} catch (RuntimeException $e) {
echo $e->getMessage(), "\n";
}

$m->attachIterator($iter1);
$m->attachIterator($iter2);
Expand Down Expand Up @@ -105,7 +114,8 @@ foreach($m as $key => $value) {
?>
--EXPECTF--
-- Default flags, no iterators --
bool(false)
Called current() on an invalid iterator
Called key() on an invalid iterator
-- Default flags, MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC --
bool(true)
array(3) {
Expand Down

0 comments on commit 42cb5b5

Please sign in to comment.