Skip to content

Commit

Permalink
Throw from SplObjectStorage::current() for invalid iterator
Browse files Browse the repository at this point in the history
Accessing key()/current() on an invalid iterator is an error
condition. Throw instead of returning null.
  • Loading branch information
nikic committed Jul 14, 2021
1 parent 9fe4966 commit 58f3f75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion ext/spl/spl_observer.c
Expand Up @@ -566,7 +566,8 @@ PHP_METHOD(SplObjectStorage, current)
}

if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
return;
zend_throw_exception(spl_ce_RuntimeException, "Called current() on invalid iterator", 0);
RETURN_THROWS();
}
ZVAL_OBJ_COPY(return_value, element->obj);
} /* }}} */
Expand Down
12 changes: 9 additions & 3 deletions ext/spl/tests/SplObjectStorage_current_empty_storage.phpt
@@ -1,14 +1,20 @@
--TEST--
Check that SplObjectStorage::current returns NULL when storage is empty
Check that SplObjectStorage::current() throws when iterator invalid
--CREDITS--
PHPNW Testfest 2009 - Simon Westcott (swestcott@gmail.com)
--FILE--
<?php

$s = new SplObjectStorage();

var_dump($s->current());
var_dump($s->valid());
try {
var_dump($s->current());
} catch (RuntimeException $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
NULL
bool(false)
Called current() on invalid iterator

0 comments on commit 58f3f75

Please sign in to comment.