diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c index c4a548e1701bf..f7a32894257b5 100644 --- a/ext/sockets/sockets.c +++ b/ext/sockets/sockets.c @@ -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); @@ -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; @@ -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); @@ -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]); @@ -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; @@ -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); } @@ -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; @@ -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(); } diff --git a/ext/sockets/tests/socket_getaddrinfo_error.phpt b/ext/sockets/tests/socket_getaddrinfo_error.phpt index 9615b5d83a9e7..c3488b540c69b 100644 --- a/ext/sockets/tests/socket_getaddrinfo_error.phpt +++ b/ext/sockets/tests/socket_getaddrinfo_error.phpt @@ -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 @@ -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"