Skip to content

Commit 665ebd7

Browse files
committed
ext/sockets: socket_sendto check port range.
close GH-17299
1 parent 72ff907 commit 665ebd7

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ PHP NEWS
8686
(David Carlier)
8787
. socket_bind() throws an exception on invalid port value.
8888
(David Carlier)
89+
. socket_sendto() throws an exception on invalid port value.
90+
(David Carlier)
8991

9092
- Standard:
9193
. Fixed crypt() tests on musl when using --with-external-libcrypt

UPGRADING

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ PHP 8.5 UPGRADE NOTES
125125
last_error to EBADF and raises an E_WARNING message.
126126

127127
- Sockets:
128-
. socket_create_listen and socket_bind throw a ValueError
129-
if the port is lower than 0 or greater than 65535.
128+
. socket_create_listen, socket_bind and socket_sendto throw a
129+
ValueError if the port is lower than 0 or greater than 65535.
130130

131131
- Zlib:
132132
. The "use_include_path" argument for the

ext/sockets/sockets.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ PHP_FUNCTION(socket_sendto)
15691569
#endif
15701570
int retval;
15711571
size_t buf_len, addr_len;
1572-
zend_long len, flags, port;
1572+
zend_long len, flags, port = 0;
15731573
bool port_is_null = 1;
15741574
char *buf, *addr;
15751575

@@ -1586,6 +1586,12 @@ PHP_FUNCTION(socket_sendto)
15861586
php_sock = Z_SOCKET_P(arg1);
15871587
ENSURE_SOCKET_VALID(php_sock);
15881588

1589+
if (port < 0 || port > USHRT_MAX) {
1590+
zend_argument_value_error(6, "must be between 0 and %u", USHRT_MAX);
1591+
RETURN_THROWS();
1592+
}
1593+
1594+
15891595
if (len < 0) {
15901596
zend_argument_value_error(3, "must be greater than or equal to 0");
15911597
RETURN_THROWS();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
socket_sendto() with invalid port
3+
--EXTENSIONS--
4+
sockets
5+
--FILE--
6+
<?php
7+
$s_c = socket_create_listen(0);
8+
try {
9+
$s_w = socket_sendto($s_c, "foo", 0, MSG_OOB, '127.0.0.1', 65536);
10+
} catch (\ValueError $e) {
11+
echo $e->getMessage() . \PHP_EOL;
12+
}
13+
try {
14+
$s_w = socket_sendto($s_c, "foo", 0, MSG_OOB, '127.0.0.1', -1);
15+
} catch (\ValueError $e) {
16+
echo $e->getMessage() . \PHP_EOL;
17+
}
18+
socket_close($s_c);
19+
?>
20+
--EXPECT--
21+
socket_sendto(): Argument #6 ($port) must be between 0 and 65535
22+
socket_sendto(): Argument #6 ($port) must be between 0 and 65535

0 commit comments

Comments
 (0)