Skip to content

Commit

Permalink
Fix socket_export_stream() with wrong protocol
Browse files Browse the repository at this point in the history
Closes GH-12310.
  • Loading branch information
twose committed Sep 29, 2023
1 parent e127f87 commit b5da98b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions NEWS
Expand Up @@ -59,6 +59,9 @@ PHP NEWS
. Fix return type of stub of xml_parse_into_struct(). (nielsdos)
. Fix memory leak when calling xml_parse_into_struct() twice. (nielsdos)

- Sockets:
. Fix socket_export_stream() with wrong protocol (twosee)

28 Sep 2023, PHP 8.1.24

- Core:
Expand Down
18 changes: 9 additions & 9 deletions ext/sockets/sockets.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions ext/sockets/tests/bug_export_stream_type.phpt
@@ -0,0 +1,12 @@
--TEST--
Bug - socket_export_stream() with wrong protocol
--EXTENSIONS--
sockets
--FILE--
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, 0);
$stream = socket_export_stream($sock);
echo stream_get_meta_data($stream)['stream_type']. "\n";
?>
--EXPECT--
udp_socket

0 comments on commit b5da98b

Please sign in to comment.