]> granicus.if.org Git - php/commitdiff
- Implemented bug/request #53427 - stream_select doesn't preserve the
authorGustavo André dos Santos Lopes <cataphract@php.net>
Tue, 30 Nov 2010 16:22:48 +0000 (16:22 +0000)
committerGustavo André dos Santos Lopes <cataphract@php.net>
Tue, 30 Nov 2010 16:22:48 +0000 (16:22 +0000)
  keys. This cannot be backported to PHP 5.3 due to a BC break. See
  UPGRADING for more information.

UPGRADING
ext/standard/streamsfuncs.c
ext/standard/tests/streams/bug53427.phpt [new file with mode: 0644]

index d1c3357eb3707c50067781ca35337f19523c4046..b8aad2f4921265453201d473b7a8cf96431bc2b0 100755 (executable)
--- a/UPGRADING
+++ b/UPGRADING
@@ -140,6 +140,10 @@ UPGRADE NOTES - PHP X.Y
   possible value. This value results in scandir() performing no sorting: on
   local filesystems, this allows files to be returned in native filesystem
   order.
+- stream_select() now preserves the keys of the passed array, be they numeric or
+  strings. This breaks code that iterated the resulting stream array using a
+  numeric index, but makes easier to identify which of the passed streams are
+  present in the result.
  
 
 ===================================
index 0ed8e10e6da13d92b37e496908b8caaf6ebcf378..5f408f50d55c361b6419cb85f2606d4f44f80e7e 100644 (file)
@@ -655,9 +655,21 @@ static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
        zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);
 
        for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(stream_array));
-                zend_hash_get_current_data(Z_ARRVAL_P(stream_array), (void **) &elem) == SUCCESS;
+                zend_hash_has_more_elements(Z_ARRVAL_P(stream_array)) == SUCCESS;
                 zend_hash_move_forward(Z_ARRVAL_P(stream_array))) {
 
+               int type;
+               char *key;
+               uint key_len;
+               ulong num_ind;
+
+               type = zend_hash_get_current_key_ex(Z_ARRVAL_P(stream_array),
+                               &key, &key_len, &num_ind, 0, NULL);
+               if (type == HASH_KEY_NON_EXISTANT ||
+                       zend_hash_get_current_data(Z_ARRVAL_P(stream_array), (void **) &elem) == FAILURE) {
+                       continue; /* should not happen */
+               }
+
                php_stream_from_zval_no_verify(stream, elem);
                if (stream == NULL) {
                        continue;
@@ -669,7 +681,12 @@ static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
                 */
                if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd >= 0) {
                        if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
-                               zend_hash_next_index_insert(new_hash, (void *)elem, sizeof(zval *), (void **)&dest_elem);
+                               if (type == HASH_KEY_IS_LONG) {
+                                       zend_hash_index_update(new_hash, num_ind, (void *)elem, sizeof(zval *), (void **)&dest_elem);
+                               } else { /* HASH_KEY_IS_STRING */
+                                       zend_hash_update(new_hash, key, key_len, (void *)elem, sizeof(zval *), (void **)&dest_elem);
+                               }
+                               
                                if (dest_elem) {
                                        zval_add_ref(dest_elem);
                                }
diff --git a/ext/standard/tests/streams/bug53427.phpt b/ext/standard/tests/streams/bug53427.phpt
new file mode 100644 (file)
index 0000000..9e2e037
--- /dev/null
@@ -0,0 +1,27 @@
+--TEST--
+Bug #53427 (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);
+--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)
+}