diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2019-06-20 14:14:46 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2019-06-20 16:22:40 +0200 |
commit | 26ac6cb6be807d9e654b4a0c9b970908e210f269 (patch) | |
tree | f40c03af27e91bb0c3f2d6f9c87896d946ab49df | |
parent | 27d3373496ec5b2e45bd7403ccde1ab891efe00b (diff) | |
download | php-git-26ac6cb6be807d9e654b4a0c9b970908e210f269.tar.gz |
Handle gethostbyname misalignment on macos
-rw-r--r-- | ext/standard/dns.c | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/ext/standard/dns.c b/ext/standard/dns.c index 3f34ba67ee..9ab1e138de 100644 --- a/ext/standard/dns.c +++ b/ext/standard/dns.c @@ -248,14 +248,21 @@ PHP_FUNCTION(gethostbynamel) } hp = php_network_gethostbyname(hostname); - if (hp == NULL || hp->h_addr_list == NULL) { + if (!hp) { RETURN_FALSE; } array_init(return_value); - for (i = 0 ; hp->h_addr_list[i] != 0 ; i++) { - in = *(struct in_addr *) hp->h_addr_list[i]; + for (i = 0; hp->h_addr_list[i] != 0 ; i++) { + /* On macos h_addr_list entries may be misaligned. */ + struct in_addr *h_addr_entry; /* Don't call this h_addr, it's a macro! */ + memcpy(&h_addr_entry, &hp->h_addr_list[i], sizeof(struct in_addr *)); + if (!h_addr_entry) { + return; + } + + in = *h_addr_entry; add_next_index_string(return_value, inet_ntoa(in)); } } @@ -265,16 +272,22 @@ PHP_FUNCTION(gethostbynamel) static zend_string *php_gethostbyname(char *name) { struct hostent *hp; + struct in_addr *h_addr_0; /* Don't call this h_addr, it's a macro! */ struct in_addr in; char *address; hp = php_network_gethostbyname(name); + if (!hp) { + return zend_string_init(name, strlen(name), 0); + } - if (!hp || !*(hp->h_addr_list)) { + /* On macos h_addr_list entries may be misaligned. */ + memcpy(&h_addr_0, &hp->h_addr_list[0], sizeof(struct in_addr *)); + if (!h_addr_0) { return zend_string_init(name, strlen(name), 0); } - memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr)); + memcpy(&in.s_addr, h_addr_0, sizeof(in.s_addr)); address = inet_ntoa(in); return zend_string_init(address, strlen(address), 0); |