Skip to content

Commit

Permalink
Preserve keys in emulate_read_fd_set()
Browse files Browse the repository at this point in the history
Keys are already preserved in the non-emulated case.
  • Loading branch information
twose authored and nikic committed Apr 8, 2019
1 parent d7b5954 commit bdac9ef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
12 changes: 8 additions & 4 deletions ext/standard/streamsfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,14 +710,16 @@ static int stream_array_emulate_read_fd_set(zval *stream_array)
zval *elem, *dest_elem, new_array;
php_stream *stream;
int ret = 0;
zend_ulong num_ind;
zend_string *key;

if (Z_TYPE_P(stream_array) != IS_ARRAY) {
return 0;
}
ZVAL_NEW_ARR(&new_array);
zend_hash_init(Z_ARRVAL(new_array), zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);

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) {
Expand All @@ -730,10 +732,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(Z_ARRVAL(new_array), elem);
if (dest_elem) {
zval_add_ref(dest_elem);
if (!key) {
dest_elem = zend_hash_index_update(Z_ARRVAL(new_array), num_ind, elem);
} else {
dest_elem = zend_hash_update(Z_ARRVAL(new_array), key, elem);
}
zval_add_ref(dest_elem);
ret++;
continue;
}
Expand Down
35 changes: 35 additions & 0 deletions ext/standard/tests/streams/stream_select_preserve_keys.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Bug #53427 + emulate_read (stream_select does not preserve keys)
--FILE--
<?php
$read[1] = fopen(__FILE__, 'r');
$read['myindex'] = reset($read);
$write = NULL;
$except = NULL;

var_dump($read);
stream_select($read, $write, $except, 0);
var_dump($read);
fread(reset($read), 1);
stream_select($read, $write, $except, 0); // // emulate_read
var_dump($read);
?>
--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)
}

0 comments on commit bdac9ef

Please sign in to comment.