Skip to content

Commit af02bde

Browse files
committed
PHP 7.3 compatibility and bugfixes
- Define new GC_ADDREF/DELREF/SET_REFCOUNT macros for older PHP versions and use them instead of direct GC reference counter access - Fixup all necessary 'long' type parameters to 'zend_long', PHP 7.3 makes it mandatory, also fixup some direct function implementations to accept the same - In php_zmq_recv(), zend_string_init() was wrongly called with third parameter as '1', marking new string with IS_STR_PERSISTENT, this caused heap corruption and/or segfaults with PHP 7.3 and could possibly cause other sorts of bugs under any 7.x version With ZVAL_STRINGL macro, this last '1' parameter meant to copy the string and was seemingly erroneously moved to zend_string_init(). zend_string_init() copies string by default, and last parameter has totally different meaning here - In poll(), flag ZVAL separation on passed arrays (PHP 7.3 makes it mandatory) - Test 19 (exception on connect callback with forced reference parameter): skip on PHP 7.1 and higher, PHP >= 7.1 started to fallback to passing argument by value instead of failing - Test 21 (warning generation from callback): it is ok, but PHP 7.3 uses 'int' instead of 'integer' for constants, so allow any word in place of the word 'integer'
1 parent f261706 commit af02bde

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

php_zmq.h

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444

4545
#include "php.h"
4646

47+
#if PHP_VERSION_ID < 70300
48+
#define GC_ADDREF(p) ++GC_REFCOUNT(p)
49+
#define GC_DELREF(p) --GC_REFCOUNT(p)
50+
#define GC_SET_REFCOUNT(p, rc) GC_REFCOUNT(p) = rc
51+
#endif
52+
4753
extern zend_module_entry zmq_module_entry;
4854
#define phpext_zmq_ptr &zmq_module_entry
4955

tests/019-callbackinvalidsignature.phpt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
--TEST--
22
Test callback edge-cases
33
--SKIPIF--
4-
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc');
5+
if (PHP_VERSION_ID >= 70100) die("skip PHP 7.1 and higher fallback to passing argument by value even when forced to reference"); ?>
56
--FILE--
67
<?php
78

tests/021-callbackwarning.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ function generate_warning($a, $b)
1313
$socket = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, 'persistent_socket', 'generate_warning');
1414

1515
--EXPECTF--
16-
Warning: in_array() expects parameter 2 to be array, integer given in %s on line %d
16+
Warning: in_array() expects parameter 2 to be array, %s given in %s on line %d
1717

zmq.c

+20-20
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ php_zmq_context *php_zmq_context_get(zend_long io_threads, zend_bool is_persiste
235235
le.type = php_zmq_context_list_entry();
236236
le.ptr = context;
237237

238-
GC_REFCOUNT(&le) = 1;
238+
GC_SET_REFCOUNT(&le, 1);
239239

240240
/* plist_key is not a persistent allocated key, thus we use str_update here */
241241
if (zend_hash_str_update_mem(&EG(persistent_list), plist_key->val, plist_key->len, &le, sizeof(le)) == NULL) {
@@ -369,7 +369,7 @@ PHP_METHOD(zmq, curvekeypair)
369369
PHP_METHOD(zmqcontext, __construct)
370370
{
371371
php_zmq_context_object *intern;
372-
long io_threads = 1;
372+
zend_long io_threads = 1;
373373
zend_bool is_persistent = 1;
374374

375375
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lb", &io_threads, &is_persistent) == FAILURE) {
@@ -495,15 +495,15 @@ PHP_METHOD(zmqcontext, getOpt)
495495
Create a new zmq socket
496496
*/
497497
static
498-
php_zmq_socket *php_zmq_socket_new(php_zmq_context *context, int type, zend_bool is_persistent)
498+
php_zmq_socket *php_zmq_socket_new(php_zmq_context *context, zend_long type, zend_bool is_persistent)
499499
{
500500
php_zmq_socket *zmq_sock;
501501

502502
zmq_sock = pecalloc(1, sizeof(php_zmq_socket), is_persistent);
503503
zmq_sock->z_socket = zmq_socket(context->z_ctx, type);
504504
zmq_sock->pid = getpid();
505505
zmq_sock->ctx = context;
506-
zmq_sock->socket_type = type;
506+
zmq_sock->socket_type = type;
507507

508508
if (!zmq_sock->z_socket) {
509509
pefree(zmq_sock, is_persistent);
@@ -535,7 +535,7 @@ void php_zmq_socket_store(php_zmq_socket *zmq_sock_p, zend_long type, zend_strin
535535
le.type = php_zmq_socket_list_entry();
536536
le.ptr = zmq_sock_p;
537537

538-
GC_REFCOUNT(&le) = 1;
538+
GC_SET_REFCOUNT(&le, 1);
539539

540540
plist_key = php_zmq_socket_plist_key(type, persistent_id, use_shared_ctx);
541541

@@ -796,7 +796,7 @@ PHP_METHOD(zmqsocket, __construct)
796796

797797
/* {{{ static zend_bool php_zmq_send(php_zmq_socket_object *intern, char *message_param, long flags)
798798
*/
799-
static zend_bool php_zmq_send(php_zmq_socket_object *intern, zend_string *message_param, long flags)
799+
static zend_bool php_zmq_send(php_zmq_socket_object *intern, zend_string *message_param, zend_long flags)
800800
{
801801
int rc, errno_;
802802
zmq_msg_t message;
@@ -828,7 +828,7 @@ static void php_zmq_sendmsg_impl(INTERNAL_FUNCTION_PARAMETERS)
828828
{
829829
php_zmq_socket_object *intern;
830830
zend_string *message_param;
831-
long flags = 0;
831+
zend_long flags = 0;
832832
zend_bool ret;
833833

834834
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &message_param, &flags) == FAILURE) {
@@ -890,7 +890,7 @@ PHP_METHOD(zmqsocket, sendmulti)
890890
zval *messages;
891891
php_zmq_socket_object *intern;
892892
int to_send, ret = 0;
893-
long flags = 0;
893+
zend_long flags = 0;
894894

895895
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|l", &messages, &flags) == FAILURE) {
896896
return;
@@ -910,7 +910,7 @@ PHP_METHOD(zmqsocket, sendmulti)
910910
/* {{{ static zend_bool php_zmq_recv(php_zmq_socket_object *intern, long flags, zval *return_value)
911911
*/
912912
static
913-
zend_string *php_zmq_recv(php_zmq_socket_object *intern, long flags)
913+
zend_string *php_zmq_recv(php_zmq_socket_object *intern, zend_long flags)
914914
{
915915
int rc, errno_;
916916
zmq_msg_t message;
@@ -933,7 +933,7 @@ zend_string *php_zmq_recv(php_zmq_socket_object *intern, long flags)
933933
return NULL;
934934
}
935935

936-
str = zend_string_init(zmq_msg_data(&message), zmq_msg_size(&message), 1);
936+
str = zend_string_init(zmq_msg_data(&message), zmq_msg_size(&message), 0);
937937
zmq_msg_close(&message);
938938
return str;
939939
}
@@ -943,7 +943,7 @@ static void php_zmq_recvmsg_impl(INTERNAL_FUNCTION_PARAMETERS)
943943
{
944944
zend_string *str = NULL;
945945
php_zmq_socket_object *intern;
946-
long flags = 0;
946+
zend_long flags = 0;
947947

948948
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) {
949949
return;
@@ -974,7 +974,7 @@ PHP_METHOD(zmqsocket, recvmulti)
974974
{
975975
php_zmq_socket_object *intern;
976976
size_t value_len;
977-
long flags = 0;
977+
zend_long flags = 0;
978978
#if ZMQ_VERSION_MAJOR < 3
979979
int64_t value;
980980
#else
@@ -1303,7 +1303,7 @@ PHP_METHOD(zmqpoll, add)
13031303
{
13041304
php_zmq_poll_object *intern;
13051305
zval *object;
1306-
long events;
1306+
zend_long events;
13071307
int error;
13081308
zend_string *key;
13091309

@@ -1423,10 +1423,10 @@ PHP_METHOD(zmqpoll, poll)
14231423
php_zmq_poll_object *intern;
14241424
zval *r_array, *w_array;
14251425

1426-
long timeout = -1;
1426+
zend_long timeout = -1;
14271427
int rc;
14281428

1429-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a!a!|l", &r_array, &w_array, &timeout) == FAILURE) {
1429+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a!/a!/|l", &r_array, &w_array, &timeout) == FAILURE) {
14301430
return;
14311431
}
14321432

@@ -1592,7 +1592,7 @@ void s_clear_device_callback (php_zmq_device_cb_t *cb)
15921592
}
15931593

15941594
static
1595-
void s_init_device_callback (php_zmq_device_cb_t *cb, zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, long timeout, zval *user_data)
1595+
void s_init_device_callback (php_zmq_device_cb_t *cb, zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zend_long timeout, zval *user_data)
15961596
{
15971597
memcpy (&cb->fci, fci, sizeof (zend_fcall_info));
15981598
memcpy (&cb->fci_cache, fci_cache, sizeof (zend_fcall_info_cache));
@@ -1615,7 +1615,7 @@ void s_init_device_callback (php_zmq_device_cb_t *cb, zend_fcall_info *fci, zend
16151615
PHP_METHOD(zmqdevice, setidletimeout)
16161616
{
16171617
php_zmq_device_object *intern;
1618-
long timeout;
1618+
zend_long timeout;
16191619

16201620
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &timeout) == FAILURE) {
16211621
return;
@@ -1644,7 +1644,7 @@ PHP_METHOD(zmqdevice, getidletimeout)
16441644
PHP_METHOD(zmqdevice, settimertimeout)
16451645
{
16461646
php_zmq_device_object *intern;
1647-
long timeout;
1647+
zend_long timeout;
16481648

16491649
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &timeout) == FAILURE) {
16501650
return;
@@ -1676,7 +1676,7 @@ PHP_METHOD(zmqdevice, setidlecallback)
16761676
zval *user_data = NULL;
16771677
zend_fcall_info fci;
16781678
zend_fcall_info_cache fci_cache;
1679-
long timeout = 0;
1679+
zend_long timeout = 0;
16801680

16811681
if (ZEND_NUM_ARGS() == 2) {
16821682
php_error_docref(NULL, E_DEPRECATED, "The signature for setIdleCallback has changed, please update your code");
@@ -1718,7 +1718,7 @@ PHP_METHOD(zmqdevice, settimercallback)
17181718
zval *user_data = NULL;
17191719
zend_fcall_info fci;
17201720
zend_fcall_info_cache fci_cache;
1721-
long timeout;
1721+
zend_long timeout;
17221722

17231723
if (zend_parse_parameters(ZEND_NUM_ARGS(), "fl|z!", &fci, &fci_cache, &timeout, &user_data) == FAILURE) {
17241724
return;

zmq_sockopt.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
/*
42
+-----------------------------------------------------------------------------------+
53
| ZMQ extension for PHP |
@@ -2033,7 +2031,7 @@ PHP_METHOD(zmqsocket, getsockopt)
20332031
PHP_METHOD(zmqsocket, setsockopt)
20342032
{
20352033
php_zmq_socket_object *intern;
2036-
long key;
2034+
zend_long key;
20372035
zval *zv;
20382036

20392037
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz/", &key, &zv) == FAILURE) {

0 commit comments

Comments
 (0)