From 08ee3462b667baf049aed43bf5f9ffbd4131d718 Mon Sep 17 00:00:00 2001 From: twosee Date: Thu, 28 Sep 2023 02:17:47 +0800 Subject: [PATCH] Fix socket_export_stream() with wrong proto --- ext/sockets/sockets.c | 18 +++++++++--------- ext/sockets/tests/bug-export-proto.phpt | 12 ++++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 ext/sockets/tests/bug-export-proto.phpt diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c index 4b269d8cb0d4d..fb7b464c31b39 100644 --- a/ext/sockets/sockets.c +++ b/ext/sockets/sockets.c @@ -2251,7 +2251,7 @@ PHP_FUNCTION(socket_export_stream) php_socket *socket; php_stream *stream = NULL; php_netstream_data_t *stream_data; - char *protocol = NULL; + const char *protocol = NULL; size_t protocollen = 0; if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zsocket, socket_ce) == FAILURE) { @@ -2287,12 +2287,12 @@ PHP_FUNCTION(socket_export_stream) if (protoid == IPPROTO_TCP) #endif { - protocol = "tcp"; - protocollen = 3; + protocol = "tcp://"; + protocollen = sizeof("tcp://") - 1; } } else if (protoid == SOCK_DGRAM) { - protocol = "udp"; - protocollen = 3; + protocol = "udp://"; + protocollen = sizeof("udp://") - 1; } #ifdef PF_UNIX } else if (socket->type == PF_UNIX) { @@ -2302,11 +2302,11 @@ PHP_FUNCTION(socket_export_stream) getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &type, &typelen); if (type == SOCK_STREAM) { - protocol = "unix"; - protocollen = 4; + protocol = "unix://"; + protocollen = sizeof("unix://") - 1; } else if (type == SOCK_DGRAM) { - protocol = "udg"; - protocollen = 3; + protocol = "udg://"; + protocollen = sizeof("udg://") - 1; } #endif } diff --git a/ext/sockets/tests/bug-export-proto.phpt b/ext/sockets/tests/bug-export-proto.phpt new file mode 100644 index 0000000000000..86e8b8e496e18 --- /dev/null +++ b/ext/sockets/tests/bug-export-proto.phpt @@ -0,0 +1,12 @@ +--TEST-- +Bug - socket_export_stream() with wrong proto +--EXTENSIONS-- +sockets +--FILE-- + +--EXPECT-- +udp_socket