Skip to content

Commit d86ec31

Browse files
committed
changes from feedback
1 parent e1f2907 commit d86ec31

File tree

2 files changed

+71
-56
lines changed

2 files changed

+71
-56
lines changed

ext/sockets/sockets.c

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,66 +2592,70 @@ PHP_FUNCTION(socket_addrinfo_lookup)
25922592
#endif
25932593

25942594
if (zhints) {
2595-
if (!HT_IS_PACKED(Z_ARRVAL_P(zhints))) {
2596-
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zhints), key, hint) {
2597-
if (key) {
2598-
bool failed = false;
2599-
if (zend_string_equals_literal(key, "ai_flags")) {
2600-
zend_long val = zval_try_get_long(hint, &failed);
2601-
if (failed) {
2602-
zend_argument_type_error(3, "\"ai_flags\" key must be of type int, %s given", zend_zval_type_name(hint));
2603-
RETURN_THROWS();
2604-
}
2605-
if (val < 0 || val > INT_MAX) {
2606-
zend_argument_value_error(3, "\"ai_flags\" key must be between 0 and %d", INT_MAX);
2607-
RETURN_THROWS();
2608-
}
2609-
hints.ai_flags = (int)val;
2610-
} else if (zend_string_equals_literal(key, "ai_socktype")) {
2611-
zend_long val = zval_try_get_long(hint, &failed);
2612-
if (failed) {
2613-
zend_argument_type_error(3, "\"ai_socktype\" key must be of type int, %s given", zend_zval_type_name(hint));
2614-
RETURN_THROWS();
2615-
}
2616-
if (val < 0 || val > INT_MAX) {
2617-
zend_argument_value_error(3, "\"ai_socktype\" key must be between 0 and %d", INT_MAX);
2618-
RETURN_THROWS();
2619-
}
2620-
hints.ai_socktype = (int)val;
2621-
} else if (zend_string_equals_literal(key, "ai_protocol")) {
2622-
zend_long val = zval_try_get_long(hint, &failed);
2623-
if (failed) {
2624-
zend_argument_type_error(3, "\"ai_protocol\" key must be of type int, %s given", zend_zval_type_name(hint));
2625-
RETURN_THROWS();
2626-
}
2627-
if (val < 0 || val > INT_MAX) {
2628-
zend_argument_value_error(3, "\"ai_protocol\" key must be between 0 and %d", INT_MAX);
2629-
RETURN_THROWS();
2630-
}
2631-
hints.ai_protocol = (int)val;
2632-
} else if (zend_string_equals_literal(key, "ai_family")) {
2633-
zend_long val = zval_try_get_long(hint, &failed);
2634-
if (failed) {
2635-
zend_argument_type_error(3, "\"ai_family\" key must be of type int, %s given", zend_zval_type_name(hint));
2636-
RETURN_THROWS();
2637-
}
2638-
if (val < 0 || val >= AF_MAX) {
2639-
zend_argument_value_error(3, "\"ai_family\" key must be between 0 and %d", AF_MAX - 1);
2640-
RETURN_THROWS();
2641-
}
2642-
hints.ai_family = (int)val;
2643-
} else {
2644-
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
2645-
"\"ai_protocol\", or \"ai_family\"");
2646-
RETURN_THROWS();
2647-
}
2648-
}
2649-
} ZEND_HASH_FOREACH_END();
2650-
} else {
2595+
if (HT_IS_PACKED(Z_ARRVAL_P(zhints))) {
26512596
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
26522597
"\"ai_protocol\", or \"ai_family\"");
26532598
RETURN_THROWS();
26542599
}
2600+
2601+
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(zhints), key, hint) {
2602+
if (key) {
2603+
bool failed = false;
2604+
if (zend_string_equals_literal(key, "ai_flags")) {
2605+
zend_long val = zval_try_get_long(hint, &failed);
2606+
if (failed) {
2607+
zend_argument_type_error(3, "\"ai_flags\" key must be of type int, %s given", zend_zval_type_name(hint));
2608+
RETURN_THROWS();
2609+
}
2610+
if (val < 0 || val > INT_MAX) {
2611+
zend_argument_value_error(3, "\"ai_flags\" key must be between 0 and %d", INT_MAX);
2612+
RETURN_THROWS();
2613+
}
2614+
hints.ai_flags = (int)val;
2615+
} else if (zend_string_equals_literal(key, "ai_socktype")) {
2616+
zend_long val = zval_try_get_long(hint, &failed);
2617+
if (failed) {
2618+
zend_argument_type_error(3, "\"ai_socktype\" key must be of type int, %s given", zend_zval_type_name(hint));
2619+
RETURN_THROWS();
2620+
}
2621+
if (val < 0 || val > INT_MAX) {
2622+
zend_argument_value_error(3, "\"ai_socktype\" key must be between 0 and %d", INT_MAX);
2623+
RETURN_THROWS();
2624+
}
2625+
hints.ai_socktype = (int)val;
2626+
} else if (zend_string_equals_literal(key, "ai_protocol")) {
2627+
zend_long val = zval_try_get_long(hint, &failed);
2628+
if (failed) {
2629+
zend_argument_type_error(3, "\"ai_protocol\" key must be of type int, %s given", zend_zval_type_name(hint));
2630+
RETURN_THROWS();
2631+
}
2632+
if (val < 0 || val > INT_MAX) {
2633+
zend_argument_value_error(3, "\"ai_protocol\" key must be between 0 and %d", INT_MAX);
2634+
RETURN_THROWS();
2635+
}
2636+
hints.ai_protocol = (int)val;
2637+
} else if (zend_string_equals_literal(key, "ai_family")) {
2638+
zend_long val = zval_try_get_long(hint, &failed);
2639+
if (failed) {
2640+
zend_argument_type_error(3, "\"ai_family\" key must be of type int, %s given", zend_zval_type_name(hint));
2641+
RETURN_THROWS();
2642+
}
2643+
if (val < 0 || val >= AF_MAX) {
2644+
zend_argument_value_error(3, "\"ai_family\" key must be between 0 and %d", AF_MAX - 1);
2645+
RETURN_THROWS();
2646+
}
2647+
hints.ai_family = (int)val;
2648+
} else {
2649+
zend_argument_value_error(3, "must only contain array keys \"ai_flags\", \"ai_socktype\", "
2650+
"\"ai_protocol\", or \"ai_family\"");
2651+
RETURN_THROWS();
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();
2657+
}
2658+
} ZEND_HASH_FOREACH_END();
26552659
}
26562660

26572661
if (getaddrinfo(ZSTR_VAL(hostname), service, &hints, &result) != 0) {

ext/sockets/tests/socket_getaddrinfo_error.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ try {
9494
} catch (\ValueError $e) {
9595
echo $e->getMessage() . PHP_EOL;
9696
}
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+
}
97107
?>
98108
--EXPECTF--
99109
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_family" key must be of type int, stdClass given
@@ -105,3 +115,4 @@ socket_addrinfo_lookup(): Argument #3 ($hints) "ai_socktype" key must be between
105115
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_flags" key must be between 0 and %d
106116
socket_addrinfo_lookup(): Argument #3 ($hints) "ai_protocol" key must be between 0 and %d
107117
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)