|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78479 gethostbyname can incorrectly return
Submitted: 2019-08-30 21:17 UTC Modified: 2019-09-04 07:32 UTC
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: ryan at arctype dot co Assigned:
Status: Open Package: *Network Functions
PHP Version: master-Git-2019-08-30 (Git) OS: Ubuntu Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
17 + 13 = ?
Subscribe to this entry?

 [2019-08-30 21:17 UTC] ryan at arctype dot co

<?php echo gethostbyname(""); ?>

<?php echo gethostbyname(""); ?>



The DNS on the host is running fine, DNS for is ok. We are however running on a dual-stack ipv4/v6 host. has an AAAA record, does not. 

I suspect that gethostbyname() is returning an AAAA record from the system call to gethostbyname(3), which returns a struct hostent: 

           struct hostent {
               char  *h_name;            /* official name of host */
               char **h_aliases;         /* alias list */
               int    h_addrtype;        /* host address type */
               int    h_length;          /* length of address */
               char **h_addr_list;       /* list of addresses */

While the PHP docs explicitly state that PHP's gethostbyname() returns the IPv4 address, the implementation does not restrict or check if the system call actually returns an AF_INET h_addrtype instead of AF_INET6, always assuming a v4 and copying sizeof(in_addr): 

According to the Linux man pages, "The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror() functions are obsolete.  Applications should use getaddrinfo(3), getnameinfo(3), and gai_strerror(3) instead."

"The  gethostbyaddr()  function  returns  a  structure  of  type  hostent  for the given host address addr of length len and address type type.  Valid address types are AF_INET and

This problem has manifested elsewhere in this Wordpress bug report from a dual-stack host:


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-31 01:44 UTC] zhangwenchao001 at gmail dot com
# chaochao @ KVM-Ubuntu in ~ [9:36:43]
$ php -a
Interactive mode enabled

php > echo gethostbyname("");

I have IPv4/IPv6, found no problems.
 [2019-09-04 07:32 UTC] ryan at arctype dot co
Reproduced with docker-compose.yml:

version: '2.4'
    image: php
      - ""
      - inet6
      - "sh"
      - "-c"
      - "echo '<?php echo gethostbyname(\"\"); echo \"\\n\"; echo gethostbyname(\"\"); echo \"\\n\"; ?>' | php"

$ docker-compose up
Recreating php-bug_php_1 ... done
Attaching to php-bug_php_1
php_1  |
php_1  |
php-bug_php_1 exited with code 0
 [2020-10-08 19:40 UTC] me at n-thumann dot de
The following pull request has been associated:

Patch Name: Fix #78479: Use gethostbyname2_r to specify AF_INET
On GitHub:
 [2022-12-06 08:47 UTC] asri dot jasemi102 at gmail dot com
If you truly need to say thank you ! We'd really appreciate and be thankful expecting you leave a decent report on the module page. This is the best procedure for guiding say thank you to this undertaking and sponsorship bundle. (
 [2022-12-07 06:01 UTC] amin dot jabari242 at gmail dot com
Keep sharing such informative post keep suggesting such post.   (
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Jun 07 09:03:43 2023 UTC