Skip to content

Commit c1d8246

Browse files
committed
fix swoole_client_select core dump on PHP7.
1 parent fc25c13 commit c1d8246

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

php7_wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ static inline int sw_zend_hash_update(HashTable *ht, char *k, int len, void *val
279279

280280
static inline int sw_zend_hash_get_current_key(HashTable *ht, char **key, uint32_t *keylen, ulong *num)
281281
{
282-
zend_string *_key_ptr;
282+
zend_string *_key_ptr = NULL;
283283
int type = zend_hash_get_current_key(ht, &_key_ptr, (zend_ulong*) num);
284284
*key = _key_ptr->val;
285285
*keylen = _key_ptr->len;

swoole_client.c

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,20 +1566,20 @@ static int client_select_wait(zval *sock_array, fd_set *fds TSRMLS_DC)
15661566
{
15671567
zval *element = NULL;
15681568
zval *zsock;
1569-
zval **dest_element;
1570-
HashTable *new_hash;
15711569
zend_class_entry *ce;
15721570

1573-
char *key = NULL;
1574-
int num = 0;
1575-
ulong_t num_key = 0;
1576-
uint32_t key_len = 0;
1577-
1571+
ulong_t num = 0;
15781572
if (SW_Z_TYPE_P(sock_array) != IS_ARRAY)
15791573
{
15801574
return 0;
15811575
}
15821576

1577+
#if PHP_MAJOR_VERSION < 7
1578+
HashTable *new_hash;
1579+
char *key = NULL;
1580+
zval **dest_element;
1581+
uint32_t key_len;
1582+
15831583
ALLOC_HASHTABLE(new_hash);
15841584
zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(sock_array)), NULL, ZVAL_PTR_DTOR, 0);
15851585

@@ -1598,13 +1598,13 @@ static int client_select_wait(zval *sock_array, fd_set *fds TSRMLS_DC)
15981598
}
15991599
if ((Z_LVAL(*zsock) < FD_SETSIZE) && FD_ISSET(Z_LVAL(*zsock), fds))
16001600
{
1601-
switch (sw_zend_hash_get_current_key(Z_ARRVAL_P(sock_array), &key, &key_len, &num_key))
1601+
switch (sw_zend_hash_get_current_key(Z_ARRVAL_P(sock_array), &key, &key_len, &num))
16021602
{
16031603
case HASH_KEY_IS_STRING:
16041604
sw_zend_hash_add(new_hash, key, key_len, (void * ) &element, sizeof(zval *), (void ** )&dest_element);
16051605
break;
16061606
case HASH_KEY_IS_LONG:
1607-
sw_zend_hash_index_update(new_hash, num_key, (void * ) &element, sizeof(zval *), (void ** )&dest_element);
1607+
sw_zend_hash_index_update(new_hash, num, (void * ) &element, sizeof(zval *), (void ** )&dest_element);
16081608
break;
16091609
}
16101610
if (dest_element)
@@ -1620,7 +1620,49 @@ static int client_select_wait(zval *sock_array, fd_set *fds TSRMLS_DC)
16201620

16211621
zend_hash_internal_pointer_reset(new_hash);
16221622
Z_ARRVAL_P(sock_array) = new_hash;
1623+
#else
1624+
zval new_array;
1625+
array_init(&new_array);
1626+
zend_ulong num_key;
1627+
zend_string *key;
1628+
zval *dest_element;
1629+
1630+
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(sock_array), num_key, key, element)
1631+
{
1632+
if (Z_TYPE_P(element) != IS_OBJECT)
1633+
{
1634+
swoole_php_fatal_error(E_WARNING, "object is not swoole_client object[1].");
1635+
continue;
1636+
}
1637+
ce = Z_OBJCE_P(element);
1638+
zsock = sw_zend_read_property(ce, element, SW_STRL("sock")-1, 0 TSRMLS_CC);
1639+
if (zsock == NULL || ZVAL_IS_NULL(zsock))
1640+
{
1641+
swoole_php_fatal_error(E_WARNING, "object is not swoole_client object[2].");
1642+
continue;
1643+
}
16231644

1645+
if ((Z_LVAL(*zsock) < FD_SETSIZE) && FD_ISSET(Z_LVAL(*zsock), fds))
1646+
{
1647+
if (key)
1648+
{
1649+
dest_element = zend_hash_add(Z_ARRVAL(new_array), key, element);
1650+
}
1651+
else
1652+
{
1653+
dest_element = zend_hash_index_update(Z_ARRVAL(new_array), num_key, element);
1654+
}
1655+
if (dest_element)
1656+
{
1657+
Z_ADDREF_P(dest_element);
1658+
}
1659+
}
1660+
num++;
1661+
} ZEND_HASH_FOREACH_END();
1662+
1663+
zval_ptr_dtor(sock_array);
1664+
ZVAL_COPY_VALUE(sock_array, &new_array);
1665+
#endif
16241666
return num ? 1 : 0;
16251667
}
16261668

0 commit comments

Comments
 (0)