Skip to content

Commit

Permalink
ext/sockets: socket_addrinfo_lookup and other few internal changes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
devnexen committed Jan 5, 2025
1 parent 7c32e41 commit 466f325
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ PHP NEWS
(David Carlier)
. socket_addrinfo_lookup throws an exception if any of the hints value
overflows. (David Carlier)
. socket_addrinfo_lookup throws an exception if one or more hints entries
has an index as numeric. (David Carlier)

- Standard:
. Fixed crypt() tests on musl when using --with-external-libcrypt
Expand Down
3 changes: 2 additions & 1 deletion UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ PHP 8.5 UPGRADE NOTES

- Sockets:
. socket_create_listen, socket_bind and socket_sendto throw a
ValueError if the port is lower than 0 or greater than 65535.
ValueError if the port is lower than 0 or greater than 65535,
also if any of the hints array entry is indexes numerically.
. socket_addrinfo_lookup throw a TypeError if any of the hints
values cannot be cast to a int and can throw a ValueError if
any of these values overflow.
Expand Down
32 changes: 22 additions & 10 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ PHP_FUNCTION(socket_get_option)
RETURN_FALSE;
}

array_init(return_value);
array_init_size(return_value, 2);

add_assoc_string(return_value, "function_set_name", tsf.function_set_name);
add_assoc_long(return_value, "pcbcnt", tsf.pcbcnt);
Expand All @@ -1764,7 +1764,7 @@ PHP_FUNCTION(socket_get_option)
RETURN_FALSE;
}

array_init(return_value);
array_init_size(return_value, 2);
add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
add_assoc_long(return_value, "l_linger", linger_val.l_linger);
return;
Expand All @@ -1786,7 +1786,7 @@ PHP_FUNCTION(socket_get_option)
tv.tv_usec = timeout ? (long)((timeout % 1000) * 1000) : 0;
#endif

array_init(return_value);
array_init_size(return_value, 2);

add_assoc_long(return_value, "sec", tv.tv_sec);
add_assoc_long(return_value, "usec", tv.tv_usec);
Expand All @@ -1808,7 +1808,7 @@ PHP_FUNCTION(socket_get_option)
RETURN_FALSE;
}

array_init(return_value);
array_init_size(return_value, 9);

add_assoc_long(return_value, "rmem_alloc", minfo[SK_MEMINFO_RMEM_ALLOC]);
add_assoc_long(return_value, "rcvbuf", minfo[SK_MEMINFO_RCVBUF]);
Expand All @@ -1833,7 +1833,7 @@ PHP_FUNCTION(socket_get_option)
RETURN_FALSE;
}

array_init(return_value);
array_init_size(return_value, 1);

add_assoc_string(return_value, "af_name", af.af_name);
return;
Expand All @@ -1857,9 +1857,11 @@ PHP_FUNCTION(socket_get_option)
RETURN_FALSE;
}

array_init(return_value);
size_t arrlen = optlen / sizeof(struct fil_info);

array_init_size(return_value, arrlen);

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

Expand Down Expand Up @@ -2589,7 +2591,13 @@ PHP_FUNCTION(socket_addrinfo_lookup)
# endif
#endif

if (zhints && !HT_IS_PACKED(Z_ARRVAL_P(zhints))) {
if (zhints) {
if (UNEXPECTED(HT_IS_PACKED(Z_ARRVAL_P(zhints)))) {
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
"\"ai_protocol\", or \"ai_family\"");
RETURN_THROWS();
}

ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zhints), key, hint) {
if (key) {
bool failed = false;
Expand Down Expand Up @@ -2639,9 +2647,13 @@ PHP_FUNCTION(socket_addrinfo_lookup)
hints.ai_family = (int)val;
} else {
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
"\"ai_protocol\", or \"ai_family\"");
"\"ai_protocol\", or \"ai_family\"");
RETURN_THROWS();
}
}
} else {
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
"\"ai_protocol\", or \"ai_family\"");
RETURN_THROWS();
}
} ZEND_HASH_FOREACH_END();
}
Expand Down
22 changes: 22 additions & 0 deletions ext/sockets/tests/socket_getaddrinfo_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ try {
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_addrinfo_lookup('127.0.0.1', 2000, [
AF_INET,
SOCK_DGRAM,
0,
0,
]);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
socket_addrinfo_lookup('127.0.0.1', 2000, array(
'ai_family' => AF_INET,
'ai_socktype' => SOCK_DGRAM,
0,
0,
));
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
?>
--EXPECTF--
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_family" key must be of type int, stdClass given
Expand All @@ -94,3 +114,5 @@ socket_addrinfo_lookup(): Argument #3 ($hints) "ai_family" key must be between 0
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_socktype" key must be between 0 and %d
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_flags" key must be between 0 and %d
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_protocol" key must be between 0 and %d
socket_addrinfo_lookup(): Argument #3 ($hints) must only contain array keys "ai_flags", "ai_socktype", "ai_protocol", or "ai_family"
socket_addrinfo_lookup(): Argument #3 ($hints) must only contain array keys "ai_flags", "ai_socktype", "ai_protocol", or "ai_family"

0 comments on commit 466f325

Please sign in to comment.