Skip to content

Commit 26ac6cb

Browse files
committed
Handle gethostbyname misalignment on macos
1 parent 27d3373 commit 26ac6cb

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

ext/standard/dns.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,21 @@ PHP_FUNCTION(gethostbynamel)
248248
}
249249

250250
hp = php_network_gethostbyname(hostname);
251-
if (hp == NULL || hp->h_addr_list == NULL) {
251+
if (!hp) {
252252
RETURN_FALSE;
253253
}
254254

255255
array_init(return_value);
256256

257-
for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) {
258-
in = *(struct in_addr *) hp->h_addr_list[i];
257+
for (i = 0; hp->h_addr_list[i] != 0 ; i++) {
258+
/* On macos h_addr_list entries may be misaligned. */
259+
struct in_addr *h_addr_entry; /* Don't call this h_addr, it's a macro! */
260+
memcpy(&h_addr_entry, &hp->h_addr_list[i], sizeof(struct in_addr *));
261+
if (!h_addr_entry) {
262+
return;
263+
}
264+
265+
in = *h_addr_entry;
259266
add_next_index_string(return_value, inet_ntoa(in));
260267
}
261268
}
@@ -265,16 +272,22 @@ PHP_FUNCTION(gethostbynamel)
265272
static zend_string *php_gethostbyname(char *name)
266273
{
267274
struct hostent *hp;
275+
struct in_addr *h_addr_0; /* Don't call this h_addr, it's a macro! */
268276
struct in_addr in;
269277
char *address;
270278

271279
hp = php_network_gethostbyname(name);
280+
if (!hp) {
281+
return zend_string_init(name, strlen(name), 0);
282+
}
272283

273-
if (!hp || !*(hp->h_addr_list)) {
284+
/* On macos h_addr_list entries may be misaligned. */
285+
memcpy(&h_addr_0, &hp->h_addr_list[0], sizeof(struct in_addr *));
286+
if (!h_addr_0) {
274287
return zend_string_init(name, strlen(name), 0);
275288
}
276289

277-
memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));
290+
memcpy(&in.s_addr, h_addr_0, sizeof(in.s_addr));
278291

279292
address = inet_ntoa(in);
280293
return zend_string_init(address, strlen(address), 0);

0 commit comments

Comments
 (0)