Skip to content

Commit a32368e

Browse files
committed
Fix socket_export_stream() with wrong proto
1 parent 07811b6 commit a32368e

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

ext/sockets/sockets.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,7 +2252,6 @@ PHP_FUNCTION(socket_export_stream)
22522252
php_stream *stream = NULL;
22532253
php_netstream_data_t *stream_data;
22542254
char *protocol = NULL;
2255-
size_t protocollen = 0;
22562255

22572256
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zsocket, socket_ce) == FAILURE) {
22582257
RETURN_THROWS();
@@ -2287,12 +2286,10 @@ PHP_FUNCTION(socket_export_stream)
22872286
if (protoid == IPPROTO_TCP)
22882287
#endif
22892288
{
2290-
protocol = "tcp";
2291-
protocollen = 3;
2289+
protocol = "tcp://";
22922290
}
22932291
} else if (protoid == SOCK_DGRAM) {
2294-
protocol = "udp";
2295-
protocollen = 3;
2292+
protocol = "udp://";
22962293
}
22972294
#ifdef PF_UNIX
22982295
} else if (socket->type == PF_UNIX) {
@@ -2302,11 +2299,9 @@ PHP_FUNCTION(socket_export_stream)
23022299
getsockopt(socket->bsd_socket, SOL_SOCKET, SO_TYPE, (char *) &type, &typelen);
23032300

23042301
if (type == SOCK_STREAM) {
2305-
protocol = "unix";
2306-
protocollen = 4;
2302+
protocol = "unix://";
23072303
} else if (type == SOCK_DGRAM) {
2308-
protocol = "udg";
2309-
protocollen = 3;
2304+
protocol = "udg://";
23102305
}
23112306
#endif
23122307
}
@@ -2315,7 +2310,7 @@ PHP_FUNCTION(socket_export_stream)
23152310
* We don't want streams to actually *do* anything though, so don't give it
23162311
* anything apart from the protocol */
23172312
if (protocol != NULL) {
2318-
stream = php_stream_xport_create(protocol, protocollen, 0, 0, NULL, NULL, NULL, NULL, NULL);
2313+
stream = php_stream_xport_create(protocol, strlen(protocol), 0, 0, NULL, NULL, NULL, NULL, NULL);
23192314
}
23202315

23212316
/* Fall back to creating a generic socket stream */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Bug - socket_export_stream() with wrong proto
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)