Skip to content

Commit b7c76de

Browse files
committed
ext/socket: socket_addrinfo_lookup check hints array.
close GH-17300
1 parent 249d2da commit b7c76de

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ PHP NEWS
8888
(David Carlier)
8989
. socket_sendto() throws an exception on invalid port value.
9090
(David Carlier)
91+
. socket_addrinfo_lookup throws an exception on invalid hints value types.
92+
(David Carlier)
9193

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

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ PHP 8.5 UPGRADE NOTES
127127
- Sockets:
128128
. socket_create_listen, socket_bind and socket_sendto throw a
129129
ValueError if the port is lower than 0 or greater than 65535.
130+
. socket_addrinfo_lookup throw a TypeError if any of the hints
131+
values cannot be cast to a int.
130132

131133
- Zlib:
132134
. The "use_include_path" argument for the

ext/sockets/sockets.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,14 +2586,35 @@ PHP_FUNCTION(socket_addrinfo_lookup)
25862586
if (zhints && !HT_IS_PACKED(Z_ARRVAL_P(zhints))) {
25872587
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zhints), key, hint) {
25882588
if (key) {
2589+
bool failed = false;
25892590
if (zend_string_equals_literal(key, "ai_flags")) {
2590-
hints.ai_flags = zval_get_long(hint);
2591+
zend_long val = zval_try_get_long(hint, &failed);
2592+
if (failed) {
2593+
zend_argument_type_error(3, "\"ai_flags\" key must be of type int, %s given", zend_zval_type_name(hint));
2594+
RETURN_THROWS();
2595+
}
2596+
hints.ai_flags = (int)val;
25912597
} else if (zend_string_equals_literal(key, "ai_socktype")) {
2592-
hints.ai_socktype = zval_get_long(hint);
2598+
zend_long val = zval_try_get_long(hint, &failed);
2599+
if (failed) {
2600+
zend_argument_type_error(3, "\"ai_socktype\" key must be of type int, %s given", zend_zval_type_name(hint));
2601+
RETURN_THROWS();
2602+
}
2603+
hints.ai_socktype = (int)val;
25932604
} else if (zend_string_equals_literal(key, "ai_protocol")) {
2594-
hints.ai_protocol = zval_get_long(hint);
2605+
zend_long val = zval_try_get_long(hint, &failed);
2606+
if (failed) {
2607+
zend_argument_type_error(3, "\"ai_protocol\" key must be of type int, %s given", zend_zval_type_name(hint));
2608+
RETURN_THROWS();
2609+
}
2610+
hints.ai_protocol = (int)val;
25952611
} else if (zend_string_equals_literal(key, "ai_family")) {
2596-
hints.ai_family = zval_get_long(hint);
2612+
zend_long val = zval_try_get_long(hint, &failed);
2613+
if (failed) {
2614+
zend_argument_type_error(3, "\"ai_family\" key must be of type int, %s given", zend_zval_type_name(hint));
2615+
RETURN_THROWS();
2616+
}
2617+
hints.ai_family = (int)val;
25972618
} else {
25982619
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
25992620
"\"ai_protocol\", or \"ai_family\"");
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
socket_addrinfo_lookup with invalid hints
3+
--EXTENSIONS--
4+
sockets
5+
--FILE--
6+
<?php
7+
try {
8+
socket_addrinfo_lookup('127.0.0.1', 2000, array(
9+
'ai_family' => new stdClass(),
10+
'ai_socktype' => SOCK_DGRAM,
11+
'ai_flags' => 0,
12+
'ai_protocol' => 0,
13+
));
14+
} catch (\TypeError $e) {
15+
echo $e->getMessage() . PHP_EOL;
16+
}
17+
try {
18+
socket_addrinfo_lookup('127.0.0.1', 2000, array(
19+
'ai_family' => AF_INET,
20+
'ai_socktype' => new stdClass(),
21+
'ai_flags' => 0,
22+
'ai_protocol' => 0,
23+
));
24+
} catch (\TypeError $e) {
25+
echo $e->getMessage() . PHP_EOL;
26+
}
27+
try {
28+
socket_addrinfo_lookup('127.0.0.1', 2000, array(
29+
'ai_family' => AF_INET,
30+
'ai_socktype' => SOCK_DGRAM,
31+
'ai_flags' => new stdClass(),
32+
'ai_protocol' => 0,
33+
));
34+
} catch (\TypeError $e) {
35+
echo $e->getMessage() . PHP_EOL;
36+
}
37+
try {
38+
socket_addrinfo_lookup('127.0.0.1', 2000, array(
39+
'ai_family' => AF_INET,
40+
'ai_socktype' => SOCK_DGRAM,
41+
'ai_flags' => 0,
42+
'ai_protocol' => new stdClass(),
43+
));
44+
} catch (\TypeError $e) {
45+
echo $e->getMessage() . PHP_EOL;
46+
}
47+
?>
48+
--EXPECT--
49+
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_family" key must be of type int, stdClass given
50+
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_socktype" key must be of type int, stdClass given
51+
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_flags" key must be of type int, stdClass given
52+
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_protocol" key must be of type int, stdClass given

0 commit comments

Comments
 (0)