Skip to content

Commit 61538eb

Browse files
Fixed bug #75097 (gethostname fails if your host name is 64 chars long)
PHP contained two different off-by-one errors, which are fixed here. First, it created a buffer of size HOST_NAME_MAX, not adding space for a null terminator. Second, it subtracted 1 from the size of that buffer when passing its size to gethostname(), despite gethostname() expecting it to be a buffer size including space for a terminating null byte, not a string length.
1 parent 3cad07b commit 61538eb

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ PHP NEWS
88
- CURL:
99
. Fixed bug #75093 (OpenSSL support not detected). (Remi)
1010

11+
- Standard:
12+
. Fixed bug #75097 (gethostname fails if your host name is 64 chars long). (Andrea)
1113

1214
31 Aug 2017 PHP 7.0.23
1315

ext/standard/dns.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ static zend_string *php_gethostbyname(char *name);
131131
Get the host name of the current machine */
132132
PHP_FUNCTION(gethostname)
133133
{
134-
char buf[HOST_NAME_MAX];
134+
char buf[HOST_NAME_MAX + 1];
135135

136136
if (zend_parse_parameters_none() == FAILURE) {
137137
return;
138138
}
139139

140-
if (gethostname(buf, sizeof(buf) - 1)) {
140+
if (gethostname(buf, sizeof(buf))) {
141141
php_error_docref(NULL, E_WARNING, "unable to fetch host [%d]: %s", errno, strerror(errno));
142142
RETURN_FALSE;
143143
}

0 commit comments

Comments
 (0)