-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix GH-16905: Internal iterator functions can't handle UNDEF properties #16907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
ca79b10
b3cc8a8
393199c
9a18fc7
031321a
4542899
165b07a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1029,11 +1029,40 @@ static inline HashTable *get_ht_for_iap(zval *zv, bool separate) { | |
return zobj->handlers->get_properties(zobj); | ||
} | ||
|
||
static void ia_return_current(zval *return_value, HashTable *array, bool forward_direction) | ||
{ | ||
zval *entry; | ||
|
||
while (true) { | ||
if ((entry = zend_hash_get_current_data(array)) == NULL) { | ||
RETURN_FALSE; | ||
} | ||
|
||
ZVAL_DEINDIRECT(entry); | ||
|
||
/* Possible with an uninitialized typed property */ | ||
if (UNEXPECTED(Z_TYPE_P(entry) == IS_UNDEF)) { | ||
zend_result result; | ||
if (forward_direction) { | ||
result = zend_hash_move_forward(array); | ||
} else { | ||
result = zend_hash_move_backwards(array); | ||
} | ||
if (result != SUCCESS) { | ||
RETURN_FALSE; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
RETURN_COPY_DEREF(entry); | ||
} | ||
|
||
/* {{{ Advances array argument's internal pointer to the last element and return it */ | ||
PHP_FUNCTION(end) | ||
{ | ||
zval *array_zv; | ||
zval *entry; | ||
|
||
ZEND_PARSE_PARAMETERS_START(1, 1) | ||
Z_PARAM_ARRAY_OR_OBJECT_EX(array_zv, 0, 1) | ||
|
@@ -1047,15 +1076,7 @@ PHP_FUNCTION(end) | |
zend_hash_internal_pointer_end(array); | ||
|
||
if (USED_RET()) { | ||
if ((entry = zend_hash_get_current_data(array)) == NULL) { | ||
RETURN_FALSE; | ||
} | ||
|
||
if (Z_TYPE_P(entry) == IS_INDIRECT) { | ||
entry = Z_INDIRECT_P(entry); | ||
} | ||
|
||
RETURN_COPY_DEREF(entry); | ||
ia_return_current(return_value, array, false); | ||
} | ||
} | ||
/* }}} */ | ||
|
@@ -1064,7 +1085,6 @@ PHP_FUNCTION(end) | |
PHP_FUNCTION(prev) | ||
{ | ||
zval *array_zv; | ||
zval *entry; | ||
|
||
ZEND_PARSE_PARAMETERS_START(1, 1) | ||
Z_PARAM_ARRAY_OR_OBJECT_EX(array_zv, 0, 1) | ||
|
@@ -1078,15 +1098,7 @@ PHP_FUNCTION(prev) | |
zend_hash_move_backwards(array); | ||
|
||
if (USED_RET()) { | ||
if ((entry = zend_hash_get_current_data(array)) == NULL) { | ||
RETURN_FALSE; | ||
} | ||
|
||
if (Z_TYPE_P(entry) == IS_INDIRECT) { | ||
entry = Z_INDIRECT_P(entry); | ||
} | ||
|
||
RETURN_COPY_DEREF(entry); | ||
ia_return_current(return_value, array, false); | ||
} | ||
} | ||
/* }}} */ | ||
|
@@ -1095,7 +1107,6 @@ PHP_FUNCTION(prev) | |
PHP_FUNCTION(next) | ||
{ | ||
zval *array_zv; | ||
zval *entry; | ||
|
||
ZEND_PARSE_PARAMETERS_START(1, 1) | ||
Z_PARAM_ARRAY_OR_OBJECT_EX(array_zv, 0, 1) | ||
|
@@ -1109,15 +1120,7 @@ PHP_FUNCTION(next) | |
zend_hash_move_forward(array); | ||
|
||
if (USED_RET()) { | ||
if ((entry = zend_hash_get_current_data(array)) == NULL) { | ||
RETURN_FALSE; | ||
} | ||
|
||
if (Z_TYPE_P(entry) == IS_INDIRECT) { | ||
entry = Z_INDIRECT_P(entry); | ||
} | ||
|
||
RETURN_COPY_DEREF(entry); | ||
ia_return_current(return_value, array, true); | ||
} | ||
} | ||
/* }}} */ | ||
|
@@ -1126,7 +1129,6 @@ PHP_FUNCTION(next) | |
PHP_FUNCTION(reset) | ||
{ | ||
zval *array_zv; | ||
zval *entry; | ||
|
||
ZEND_PARSE_PARAMETERS_START(1, 1) | ||
Z_PARAM_ARRAY_OR_OBJECT_EX(array_zv, 0, 1) | ||
|
@@ -1140,15 +1142,7 @@ PHP_FUNCTION(reset) | |
zend_hash_internal_pointer_reset(array); | ||
|
||
if (USED_RET()) { | ||
if ((entry = zend_hash_get_current_data(array)) == NULL) { | ||
RETURN_FALSE; | ||
} | ||
|
||
if (Z_TYPE_P(entry) == IS_INDIRECT) { | ||
entry = Z_INDIRECT_P(entry); | ||
} | ||
|
||
RETURN_COPY_DEREF(entry); | ||
ia_return_current(return_value, array, true); | ||
} | ||
} | ||
/* }}} */ | ||
|
@@ -1157,22 +1151,13 @@ PHP_FUNCTION(reset) | |
PHP_FUNCTION(current) | ||
{ | ||
zval *array_zv; | ||
zval *entry; | ||
|
||
ZEND_PARSE_PARAMETERS_START(1, 1) | ||
Z_PARAM_ARRAY_OR_OBJECT(array_zv) | ||
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
HashTable *array = get_ht_for_iap(array_zv, /* separate */ false); | ||
if ((entry = zend_hash_get_current_data(array)) == NULL) { | ||
RETURN_FALSE; | ||
} | ||
|
||
if (Z_TYPE_P(entry) == IS_INDIRECT) { | ||
entry = Z_INDIRECT_P(entry); | ||
} | ||
|
||
RETURN_COPY_DEREF(entry); | ||
ia_return_current(return_value, array, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will implicitly advance when the current element is unset. E.g. with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's fair, same for key(), let me fix this. |
||
} | ||
/* }}} */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--TEST-- | ||
GH-16905 (Internal iterator functions can't handle UNDEF properties) | ||
--FILE-- | ||
<?php | ||
|
||
class TestSomeUndef { | ||
public int $a; | ||
public int $b; | ||
public int $c; | ||
public int $d; | ||
} | ||
|
||
class TestAllUndef { | ||
public int $a; | ||
} | ||
|
||
$x = new TestSomeUndef; | ||
$x->b = 1; | ||
$x->c = 2; | ||
|
||
var_dump(reset($x)); | ||
var_dump(current($x)); | ||
var_dump(end($x)); | ||
|
||
var_dump(reset($x)); | ||
var_dump(next($x)); | ||
|
||
var_dump(end($x)); | ||
var_dump(prev($x)); | ||
|
||
var_dump(value: key($x)); | ||
|
||
$x = new TestAllUndef; | ||
var_dump(current($x)); | ||
|
||
?> | ||
--EXPECTF-- | ||
Deprecated: reset(): Calling reset() on an object is deprecated in %s on line %d | ||
int(1) | ||
|
||
Deprecated: current(): Calling current() on an object is deprecated in %s on line %d | ||
int(1) | ||
|
||
Deprecated: end(): Calling end() on an object is deprecated in %s on line %d | ||
int(2) | ||
|
||
Deprecated: reset(): Calling reset() on an object is deprecated in %s on line %d | ||
int(1) | ||
|
||
Deprecated: next(): Calling next() on an object is deprecated in %s on line %d | ||
int(2) | ||
|
||
Deprecated: end(): Calling end() on an object is deprecated in %s on line %d | ||
int(2) | ||
|
||
Deprecated: prev(): Calling prev() on an object is deprecated in %s on line %d | ||
int(1) | ||
|
||
Deprecated: key(): Calling key() on an object is deprecated in %s on line %d | ||
string(1) "b" | ||
|
||
Deprecated: current(): Calling current() on an object is deprecated in %s on line %d | ||
bool(false) |
Uh oh!
There was an error while loading. Please reload this page.