Skip to content

Commit b692f4a

Browse files
committed
ext/sockets: GH-20532 socket_addrinfo_lookup() return EAI error code on
resolution failures.
1 parent 9e6acb4 commit b692f4a

File tree

6 files changed

+229
-7
lines changed

6 files changed

+229
-7
lines changed

UPGRADING

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ PHP 8.6 UPGRADE NOTES
5757
- Phar:
5858
. Phar::mungServer() now supports reference values.
5959

60+
- Sockets:
61+
. socket_addrinfo_lookup() now returns an error code instead of FALSE on resolution failures.
62+
6063
- Zip:
6164
. ZipArchive::extractTo now raises a TypeError for the
6265
files argument if one or more of the entries is not
@@ -85,6 +88,25 @@ PHP 8.6 UPGRADE NOTES
8588
10. New Global Constants
8689
========================================
8790

91+
- Sockets:
92+
. EAI_BADFLAGS.
93+
. EAI_NONAME.
94+
. EAI_AGAIN.
95+
. EAI_FAIL.
96+
. EAI_NODATA.
97+
. EAI_FAMILY.
98+
. EAI_SOCKTYPE.
99+
. EAI_SERVICE.
100+
. EAI_ADDRFAMILY.
101+
. EAI_SYSTEM.
102+
. EAI_OVERFLOW
103+
. EAI_INPROGRESS.
104+
. EAI_CANCELED.
105+
. EAI_NOTCANCELED.
106+
. EAI_ALLDONE.
107+
. EAI_INTR.
108+
. EAI_IDN_ENCODE.
109+
88110
========================================
89111
11. Changes to INI File Handling
90112
========================================

Zend/Optimizer/zend_func_infos.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ static const func_info_t func_infos[] = {
373373
F1("session_cache_limiter", MAY_BE_STRING|MAY_BE_FALSE),
374374
F1("socket_get_option", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_LONG|MAY_BE_FALSE),
375375
FN("socket_export_stream", MAY_BE_RESOURCE|MAY_BE_FALSE),
376-
F1("socket_addrinfo_lookup", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_FALSE),
376+
F1("socket_addrinfo_lookup", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_LONG),
377377
F1("socket_addrinfo_explain", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_ARRAY),
378378
FN("sodium_crypto_kx_client_session_keys", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
379379
FN("sodium_crypto_kx_server_session_keys", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),

ext/sockets/sockets.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,6 +2731,7 @@ PHP_FUNCTION(socket_addrinfo_lookup)
27312731
size_t service_len = 0;
27322732
zend_string *hostname, *key;
27332733
zval *hint, *zhints = NULL;
2734+
int ret = 0;
27342735

27352736
struct addrinfo hints, *result, *rp;
27362737
php_addrinfo *res;
@@ -2825,8 +2826,8 @@ PHP_FUNCTION(socket_addrinfo_lookup)
28252826
} ZEND_HASH_FOREACH_END();
28262827
}
28272828

2828-
if (getaddrinfo(ZSTR_VAL(hostname), service, &hints, &result) != 0) {
2829-
RETURN_FALSE;
2829+
if ((ret = getaddrinfo(ZSTR_VAL(hostname), service, &hints, &result)) != 0) {
2830+
RETURN_LONG(ret);
28302831
}
28312832

28322833
array_init(return_value);

ext/sockets/sockets.stub.php

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,136 @@
20532053
const SHUT_RDWR = UNKNOWN;
20542054
#endif
20552055

2056+
2057+
#ifdef EAI_BADFLAGS
2058+
/**
2059+
* @var int
2060+
* @cvalue EAI_BADFLAGS
2061+
*/
2062+
const EAI_BADFLAGS = UNKNOWN;
2063+
#endif
2064+
#ifdef EAI_NONAME
2065+
/**
2066+
* @var int
2067+
* @cvalue EAI_NONAME
2068+
*/
2069+
const EAI_NONAME = UNKNOWN;
2070+
#endif
2071+
#ifdef EAI_AGAIN
2072+
/**
2073+
* @var int
2074+
* @cvalue EAI_AGAIN
2075+
*/
2076+
const EAI_AGAIN = UNKNOWN;
2077+
#endif
2078+
#ifdef EAI_FAIL
2079+
/**
2080+
* @var int
2081+
* @cvalue EAI_FAIL
2082+
*/
2083+
const EAI_FAIL = UNKNOWN;
2084+
#endif
2085+
#ifdef EAI_NODATA
2086+
/**
2087+
* @var int
2088+
* @cvalue EAI_NODATA
2089+
*/
2090+
const EAI_NODATA = UNKNOWN;
2091+
#endif
2092+
#ifdef EAI_FAMILY
2093+
/**
2094+
* @var int
2095+
* @cvalue EAI_FAMILY
2096+
*/
2097+
const EAI_FAMILY = UNKNOWN;
2098+
#endif
2099+
#ifdef EAI_SOCKTYPE
2100+
/**
2101+
* @var int
2102+
* @cvalue EAI_SOCKTYPE
2103+
*/
2104+
const EAI_SOCKTYPE = UNKNOWN;
2105+
#endif
2106+
#ifdef EAI_SERVICE
2107+
/**
2108+
* @var int
2109+
* @cvalue EAI_SERVICE
2110+
*/
2111+
const EAI_SERVICE = UNKNOWN;
2112+
#endif
2113+
#ifdef EAI_ADDRFAMILY
2114+
/**
2115+
* @var int
2116+
* @cvalue EAI_ADDRFAMILY
2117+
*/
2118+
const EAI_ADDRFAMILY = UNKNOWN;
2119+
#else
2120+
#ifdef EAI_FAMILY
2121+
/**
2122+
* @var int
2123+
* @cvalue EAI_FAMILY
2124+
*/
2125+
const EAI_ADDRFAMILY = UNKNOWN;
2126+
#else
2127+
#endif
2128+
#endif
2129+
#ifdef EAI_SYSTEM
2130+
/**
2131+
* @var int
2132+
* @cvalue EAI_SYSTEM
2133+
*/
2134+
const EAI_SYSTEM = UNKNOWN;
2135+
#endif
2136+
#ifdef EAI_OVERFLOW
2137+
/**
2138+
* @var int
2139+
* @cvalue EAI_OVERFLOW
2140+
*/
2141+
const EAI_OVERFLOW = UNKNOWN;
2142+
#endif
2143+
#ifdef EAI_INPROGRESS
2144+
/**
2145+
* @var int
2146+
* @cvalue EAI_INPROGRESS
2147+
*/
2148+
const EAI_INPROGRESS = UNKNOWN;
2149+
#endif
2150+
#ifdef EAI_CANCELED
2151+
/**
2152+
* @var int
2153+
* @cvalue EAI_CANCELED
2154+
*/
2155+
const EAI_CANCELED = UNKNOWN;
2156+
#endif
2157+
#ifdef EAI_NOTCANCELED
2158+
/**
2159+
* @var int
2160+
* @cvalue EAI_NOTCANCELED
2161+
*/
2162+
const EAI_NOTCANCELED = UNKNOWN;
2163+
#endif
2164+
#ifdef EAI_ALLDONE
2165+
/**
2166+
* @var int
2167+
* @cvalue EAI_ALLDONE
2168+
*/
2169+
const EAI_ALLDONE = UNKNOWN;
2170+
#endif
2171+
#ifdef EAI_INTR
2172+
/**
2173+
* @var int
2174+
* @cvalue EAI_INTR
2175+
*/
2176+
const EAI_INTR = UNKNOWN;
2177+
#endif
2178+
#ifdef EAI_IDN_ENCODE
2179+
/**
2180+
* @var int
2181+
* @cvalue EAI_IDN_ENCODE
2182+
*/
2183+
const EAI_IDN_ENCODE = UNKNOWN;
2184+
#endif
2185+
20562186
/**
20572187
* @strict-properties
20582188
* @not-serializable
@@ -2172,10 +2302,10 @@ function socket_recvmsg(Socket $socket, array &$message, int $flags = 0): int|fa
21722302
function socket_cmsg_space(int $level, int $type, int $num = 0): ?int {}
21732303

21742304
/**
2175-
* @return array<int, AddressInfo>|false
2305+
* @return array<int, AddressInfo>|int
21762306
* @refcount 1
21772307
*/
2178-
function socket_addrinfo_lookup(string $host, ?string $service = null, array $hints = []): array|false {}
2308+
function socket_addrinfo_lookup(string $host, ?string $service = null, array $hints = []): array|int {}
21792309

21802310
function socket_addrinfo_connect(AddressInfo $address): Socket|false {}
21812311

ext/sockets/sockets_arginfo.h

Lines changed: 56 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/sockets/tests/gh20532.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-20562 - socket_addrinfo_lookup() returns error codes on resolution failures.
3+
--EXTENSIONS--
4+
sockets
5+
--FILE--
6+
<?php
7+
var_dump(socket_addrinfo_lookup(".whynot") == EAI_NONAME);
8+
var_dump(in_array(socket_addrinfo_lookup("2001:db8::1", null, ['ai_family' => AF_INET]), [EAI_FAMILY, EAI_ADDRFAMILY, EAI_NONAME, EAI_NODATA]));
9+
var_dump(in_array(socket_addrinfo_lookup("example.com", "http", ['ai_socktype' => SOCK_RAW, 'ai_flags' => 2147483647]), [EAI_SOCKTYPE, EAI_SERVICE, EAI_BADFLAGS, EAI_NONAME]));
10+
?>
11+
--EXPECT--
12+
bool(true)
13+
bool(true)
14+
bool(true)
15+

0 commit comments

Comments
 (0)