|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75097 gethostname fails if your host name is 64 chars long
Submitted: 2017-08-19 19:01 UTC Modified: 2017-08-19 19:58 UTC
From: james at jmbtechnology dot co dot uk Assigned:
Status: Closed Package: Network related
PHP Version: 7.0.22 OS: Ubuntu 17.04
Private report: No CVE-ID:
 [2017-08-19 19:01 UTC] james at jmbtechnology dot co dot uk
A PHP library recently started running gethostname() on my live production server, and crashing with the error:

PHP Warning:  gethostname(): unable to fetch host [36]: File name too long in /thePHPFile.php on line 17

Googling lead me to:

And the following comment:

"According to my tests, this is more +-1 error than anything else. The problem is relevant only when the hostname is precisely 64 bytes long, in which case gethostname(lname, MAXHOSTNAMELEN) (where MAXHOSTNAMELEN is 64) can not accomodate terminating byte. "

I checked, and the host name was exactly 64 chars! The staging server hostname was not, and the library ran fine there. Quickly, I renamed the live server hostname to be 63 characters and it worked fine!

Tested on a VM I had to - see test script.

I don't know enough to know if this is a upstream bug - sorry! But as the effect manifests in PHP, it seemed worth reporting. Also can't currently test if this occurs in latest version of PHP - sorry again - but if I get anywhere with that later will update.

Test script:
Test it failing

vagrant@vagrant:/vagrant/build$ sudo hostname 1234567890123456789012345678901234567890123456789012345678901234
sudo: unable to resolve host 12345678901234567890123456789012345678901234567890123456789012
vagrant@vagrant:/vagrant/build$ php /home/vagrant/test.php 
PHP Warning:  gethostname(): unable to fetch host [36]: File name too long in /home/vagrant/test.php on line 3

Test it passing

vagrant@vagrant:/vagrant/build$ sudo hostname 123456789012345678901234567890123456789012345678901234567890    
sudo: unable to resolve host 1234567890123456789012345678901234567890123456789012345678901234
vagrant@vagrant:/vagrant/build$ php /home/vagrant/test.php 

And some background info

vagrant@vagrant:/vagrant/build$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04 LTS
Release:	16.04
Codename:	xenial
vagrant@vagrant:/vagrant/build$ php -v        
PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.22-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies
vagrant@vagrant:/vagrant/build$ cat /home/vagrant/test.php 

echo gethostname();

Expected result:
Expect gethostname() to return result even with long host names.

Actual result:
PHP Warning:  gethostname(): unable to fetch host [36]: File name too long in /home/vagrant/test.php on line 3

And nothing returned


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-08-19 19:39 UTC]
Automatic comment on behalf of
Log: Fixed bug #75097 (gethostname fails if your host name is 64 chars long)
 [2017-08-19 19:39 UTC]
-Status: Open +Status: Closed
 [2017-08-19 19:58 UTC] james at jmbtechnology dot co dot uk
Thanks Andrea! :-)
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC