Skip to content

Commit 466f325

Browse files
committed
ext/sockets: socket_addrinfo_lookup and other few internal changes
- socket_addrinfo_lookup throws when hints is an indexed array. - socket_get_option hardcoding size outputs to user when data size known. close GH-17363
1 parent 7c32e41 commit 466f325

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ PHP NEWS
9292
(David Carlier)
9393
. socket_addrinfo_lookup throws an exception if any of the hints value
9494
overflows. (David Carlier)
95+
. socket_addrinfo_lookup throws an exception if one or more hints entries
96+
has an index as numeric. (David Carlier)
9597

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

UPGRADING

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ PHP 8.5 UPGRADE NOTES
126126

127127
- Sockets:
128128
. socket_create_listen, socket_bind and socket_sendto throw a
129-
ValueError if the port is lower than 0 or greater than 65535.
129+
ValueError if the port is lower than 0 or greater than 65535,
130+
also if any of the hints array entry is indexes numerically.
130131
. socket_addrinfo_lookup throw a TypeError if any of the hints
131132
values cannot be cast to a int and can throw a ValueError if
132133
any of these values overflow.

ext/sockets/sockets.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,7 @@ PHP_FUNCTION(socket_get_option)
17411741
RETURN_FALSE;
17421742
}
17431743

1744-
array_init(return_value);
1744+
array_init_size(return_value, 2);
17451745

17461746
add_assoc_string(return_value, "function_set_name", tsf.function_set_name);
17471747
add_assoc_long(return_value, "pcbcnt", tsf.pcbcnt);
@@ -1764,7 +1764,7 @@ PHP_FUNCTION(socket_get_option)
17641764
RETURN_FALSE;
17651765
}
17661766

1767-
array_init(return_value);
1767+
array_init_size(return_value, 2);
17681768
add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
17691769
add_assoc_long(return_value, "l_linger", linger_val.l_linger);
17701770
return;
@@ -1786,7 +1786,7 @@ PHP_FUNCTION(socket_get_option)
17861786
tv.tv_usec = timeout ? (long)((timeout % 1000) * 1000) : 0;
17871787
#endif
17881788

1789-
array_init(return_value);
1789+
array_init_size(return_value, 2);
17901790

17911791
add_assoc_long(return_value, "sec", tv.tv_sec);
17921792
add_assoc_long(return_value, "usec", tv.tv_usec);
@@ -1808,7 +1808,7 @@ PHP_FUNCTION(socket_get_option)
18081808
RETURN_FALSE;
18091809
}
18101810

1811-
array_init(return_value);
1811+
array_init_size(return_value, 9);
18121812

18131813
add_assoc_long(return_value, "rmem_alloc", minfo[SK_MEMINFO_RMEM_ALLOC]);
18141814
add_assoc_long(return_value, "rcvbuf", minfo[SK_MEMINFO_RCVBUF]);
@@ -1833,7 +1833,7 @@ PHP_FUNCTION(socket_get_option)
18331833
RETURN_FALSE;
18341834
}
18351835

1836-
array_init(return_value);
1836+
array_init_size(return_value, 1);
18371837

18381838
add_assoc_string(return_value, "af_name", af.af_name);
18391839
return;
@@ -1857,9 +1857,11 @@ PHP_FUNCTION(socket_get_option)
18571857
RETURN_FALSE;
18581858
}
18591859

1860-
array_init(return_value);
1860+
size_t arrlen = optlen / sizeof(struct fil_info);
1861+
1862+
array_init_size(return_value, arrlen);
18611863

1862-
for (i = 0; i < optlen / sizeof(struct fil_info); i++) {
1864+
for (i = 0; i < arrlen; i++) {
18631865
add_index_string(return_value, i, fi[i].fi_name);
18641866
}
18651867

@@ -2589,7 +2591,13 @@ PHP_FUNCTION(socket_addrinfo_lookup)
25892591
# endif
25902592
#endif
25912593

2592-
if (zhints && !HT_IS_PACKED(Z_ARRVAL_P(zhints))) {
2594+
if (zhints) {
2595+
if (UNEXPECTED(HT_IS_PACKED(Z_ARRVAL_P(zhints)))) {
2596+
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
2597+
"\"ai_protocol\", or \"ai_family\"");
2598+
RETURN_THROWS();
2599+
}
2600+
25932601
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zhints), key, hint) {
25942602
if (key) {
25952603
bool failed = false;
@@ -2639,9 +2647,13 @@ PHP_FUNCTION(socket_addrinfo_lookup)
26392647
hints.ai_family = (int)val;
26402648
} else {
26412649
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
2642-
"\"ai_protocol\", or \"ai_family\"");
2650+
"\"ai_protocol\", or \"ai_family\"");
26432651
RETURN_THROWS();
2644-
}
2652+
}
2653+
} else {
2654+
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
2655+
"\"ai_protocol\", or \"ai_family\"");
2656+
RETURN_THROWS();
26452657
}
26462658
} ZEND_HASH_FOREACH_END();
26472659
}

ext/sockets/tests/socket_getaddrinfo_error.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ try {
8484
} catch (\ValueError $e) {
8585
echo $e->getMessage() . PHP_EOL;
8686
}
87+
try {
88+
socket_addrinfo_lookup('127.0.0.1', 2000, [
89+
AF_INET,
90+
SOCK_DGRAM,
91+
0,
92+
0,
93+
]);
94+
} catch (\ValueError $e) {
95+
echo $e->getMessage() . PHP_EOL;
96+
}
97+
try {
98+
socket_addrinfo_lookup('127.0.0.1', 2000, array(
99+
'ai_family' => AF_INET,
100+
'ai_socktype' => SOCK_DGRAM,
101+
0,
102+
0,
103+
));
104+
} catch (\ValueError $e) {
105+
echo $e->getMessage() . PHP_EOL;
106+
}
87107
?>
88108
--EXPECTF--
89109
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_family" key must be of type int, stdClass given
@@ -94,3 +114,5 @@ socket_addrinfo_lookup(): Argument #3 ($hints) "ai_family" key must be between 0
94114
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_socktype" key must be between 0 and %d
95115
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_flags" key must be between 0 and %d
96116
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_protocol" key must be between 0 and %d
117+
socket_addrinfo_lookup(): Argument #3 ($hints) must only contain array keys "ai_flags", "ai_socktype", "ai_protocol", or "ai_family"
118+
socket_addrinfo_lookup(): Argument #3 ($hints) must only contain array keys "ai_flags", "ai_socktype", "ai_protocol", or "ai_family"

0 commit comments

Comments
 (0)