Skip to content

Commit 58f3f75

Browse files
committed
Throw from SplObjectStorage::current() for invalid iterator
Accessing key()/current() on an invalid iterator is an error condition. Throw instead of returning null.
1 parent 9fe4966 commit 58f3f75

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

ext/spl/spl_observer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,8 @@ PHP_METHOD(SplObjectStorage, current)
566566
}
567567

568568
if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
569-
return;
569+
zend_throw_exception(spl_ce_RuntimeException, "Called current() on invalid iterator", 0);
570+
RETURN_THROWS();
570571
}
571572
ZVAL_OBJ_COPY(return_value, element->obj);
572573
} /* }}} */
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
--TEST--
2-
Check that SplObjectStorage::current returns NULL when storage is empty
2+
Check that SplObjectStorage::current() throws when iterator invalid
33
--CREDITS--
44
PHPNW Testfest 2009 - Simon Westcott (swestcott@gmail.com)
55
--FILE--
66
<?php
77

88
$s = new SplObjectStorage();
99

10-
var_dump($s->current());
10+
var_dump($s->valid());
11+
try {
12+
var_dump($s->current());
13+
} catch (RuntimeException $e) {
14+
echo $e->getMessage(), "\n";
15+
}
1116

1217
?>
1318
--EXPECT--
14-
NULL
19+
bool(false)
20+
Called current() on invalid iterator

0 commit comments

Comments
 (0)