diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 8fe7b2d2253..683932216da 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -694,13 +694,15 @@ static int stream_array_emulate_read_fd_set(zval *stream_array) HashTable *ht; php_stream *stream; int ret = 0; + zend_ulong num_ind; + zend_string *key; if (Z_TYPE_P(stream_array) != IS_ARRAY) { return 0; } ht = zend_new_array(zend_hash_num_elements(Z_ARRVAL_P(stream_array))); - ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(stream_array), elem) { + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(stream_array), num_ind, key, elem) { ZVAL_DEREF(elem); php_stream_from_zval_no_verify(stream, elem); if (stream == NULL) { @@ -713,10 +715,12 @@ static int stream_array_emulate_read_fd_set(zval *stream_array) * This branch of code also allows blocking streams with buffered data to * operate correctly in stream_select. * */ - dest_elem = zend_hash_next_index_insert(ht, elem); - if (dest_elem) { - zval_add_ref(dest_elem); + if (!key) { + dest_elem = zend_hash_index_update(ht, num_ind, elem); + } else { + dest_elem = zend_hash_update(ht, key, elem); } + zval_add_ref(dest_elem); ret++; continue; } diff --git a/ext/standard/tests/streams/stream_select_preserve_keys.phpt b/ext/standard/tests/streams/stream_select_preserve_keys.phpt new file mode 100644 index 00000000000..390709faf41 --- /dev/null +++ b/ext/standard/tests/streams/stream_select_preserve_keys.phpt @@ -0,0 +1,35 @@ +--TEST-- +Bug #53427 + emulate_read (stream_select does not preserve keys) +--FILE-- + +--EXPECTF-- +array(2) { + [1]=> + resource(%d) of type (stream) + ["myindex"]=> + resource(%d) of type (stream) +} +array(2) { + [1]=> + resource(%d) of type (stream) + ["myindex"]=> + resource(%d) of type (stream) +} +array(2) { + [1]=> + resource(%d) of type (stream) + ["myindex"]=> + resource(%d) of type (stream) +}