Skip to content

Commit b5da98b

Browse files
committed
Fix socket_export_stream() with wrong protocol
Closes GH-12310.
1 parent e127f87 commit b5da98b

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ PHP NEWS
5959
. Fix return type of stub of xml_parse_into_struct(). (nielsdos)
6060
. Fix memory leak when calling xml_parse_into_struct() twice. (nielsdos)
6161

62+
- Sockets:
63+
. Fix socket_export_stream() with wrong protocol (twosee)
64+
6265
28 Sep 2023, PHP 8.1.24
6366

6467
- Core:

ext/sockets/sockets.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,7 @@ PHP_FUNCTION(socket_export_stream)
22512251
php_socket *socket;
22522252
php_stream *stream = NULL;
22532253
php_netstream_data_t *stream_data;
2254-
char *protocol = NULL;
2254+
const char *protocol = NULL;
22552255
size_t protocollen = 0;
22562256

22572257
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zsocket, socket_ce) == FAILURE) {
@@ -2287,12 +2287,12 @@ PHP_FUNCTION(socket_export_stream)
22872287
if (protoid == IPPROTO_TCP)
22882288
#endif
22892289
{
2290-
protocol = "tcp";
2291-
protocollen = 3;
2290+
protocol = "tcp://";
2291+
protocollen = sizeof("tcp://") - 1;
22922292
}
22932293
} else if (protoid == SOCK_DGRAM) {
2294-
protocol = "udp";
2295-
protocollen = 3;
2294+
protocol = "udp://";
2295+
protocollen = sizeof("udp://") - 1;
22962296
}
22972297
#ifdef PF_UNIX
22982298
} else if (socket->type == PF_UNIX) {
@@ -2302,11 +2302,11 @@ PHP_FUNCTION(socket_export_stream)
23022302
getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &type, &typelen);
23032303

23042304
if (type == SOCK_STREAM) {
2305-
protocol = "unix";
2306-
protocollen = 4;
2305+
protocol = "unix://";
2306+
protocollen = sizeof("unix://") - 1;
23072307
} else if (type == SOCK_DGRAM) {
2308-
protocol = "udg";
2309-
protocollen = 3;
2308+
protocol = "udg://";
2309+
protocollen = sizeof("udg://") - 1;
23102310
}
23112311
#endif
23122312
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Bug - socket_export_stream() with wrong protocol
3+
--EXTENSIONS--
4+
sockets
5+
--FILE--
6+
<?php
7+
$sock = socket_create(AF_INET, SOCK_DGRAM, 0);
8+
$stream = socket_export_stream($sock);
9+
echo stream_get_meta_data($stream)['stream_type']. "\n";
10+
?>
11+
--EXPECT--
12+
udp_socket

0 commit comments

Comments
 (0)