Skip to content

Commit 46f9fed

Browse files
committed
Handle ref return from Iterator::key()
Handle this in the implementation of get_current_key of user_it, so that the callers may assume that the key is not a reference. Fixes oss-fuzz #33018.
1 parent f40c8fd commit 46f9fed

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Zend/tests/iterator_key_by_ref.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Iterator::key() with by-ref return
3+
--FILE--
4+
<?php
5+
class Test extends ArrayIterator {
6+
function &key() {
7+
return $foo;
8+
}
9+
}
10+
foreach (new Test([0]) as $k => $v) {
11+
var_dump($k);
12+
}
13+
?>
14+
--EXPECT--
15+
NULL

Zend/zend_interfaces.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ ZEND_API void zend_user_it_get_current_key(zend_object_iterator *_iter, zval *ke
154154
zend_user_iterator *iter = (zend_user_iterator*)_iter;
155155
zval *object = &iter->it.data;
156156
zend_call_method_with_0_params(Z_OBJ_P(object), iter->ce, &iter->ce->iterator_funcs_ptr->zf_key, "key", key);
157+
if (UNEXPECTED(Z_ISREF_P(key))) {
158+
zend_unwrap_reference(key);
159+
}
157160
}
158161
/* }}} */
159162

0 commit comments

Comments
 (0)