b8524a
From e41ee7cf3347ced6e689c198dbf3c5900009d70f Mon Sep 17 00:00:00 2001
b8524a
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
b8524a
Date: Thu, 25 Feb 2016 10:58:02 +0000
b8524a
Subject: [PATCH] merge revision(s) 53677: [Backport #11877]
b8524a
b8524a
        * ext/socket/socket.c (sock_gethostname): support unlimited size
b8524a
          hostname.
b8524a
b8524a
b8524a
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@53936 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
b8524a
---
b8524a
 ChangeLog           |  5 +++++
b8524a
 ext/socket/socket.c | 26 ++++++++++++++++++++------
b8524a
 version.h           |  2 +-
b8524a
 3 files changed, 26 insertions(+), 7 deletions(-)
b8524a
b8524a
diff --git a/ChangeLog b/ChangeLog
b8524a
index f407b73..2216dd4 100644
b8524a
--- a/ChangeLog
b8524a
+++ b/ChangeLog
b8524a
@@ -1,3 +1,8 @@
b8524a
+Thu Feb 25 19:49:31 2016  Nobuyoshi Nakada  <nobu@ruby-lang.org>
b8524a
+
b8524a
+	* ext/socket/socket.c (sock_gethostname): support unlimited size
b8524a
+	  hostname.
b8524a
+
b8524a
 Sat Feb 13 17:11:58 2016  Fabian Wiesel  <fabian.wiesel@sap.com>
b8524a
b8524a
 	* lib/uri/generic.rb (find_proxy): exclude white-spaces and allow
b8524a
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
b8524a
index 0592432..13006ab 100644
b8524a
--- a/ext/socket/socket.c
b8524a
+++ b/ext/socket/socket.c
b8524a
@@ -898,14 +898,28 @@ sock_gethostname(VALUE obj)
b8524a
 #ifndef HOST_NAME_MAX
b8524a
 #  define HOST_NAME_MAX 1024
b8524a
 #endif
b8524a
-    char buf[HOST_NAME_MAX+1];
b8524a
+    long len = HOST_NAME_MAX;
b8524a
+    VALUE name;
b8524a
 
b8524a
     rb_secure(3);
b8524a
-    if (gethostname(buf, (int)sizeof buf - 1) < 0)
b8524a
-	rb_sys_fail("gethostname");
b8524a
-
b8524a
-    buf[sizeof buf - 1] = '\0';
b8524a
-    return rb_str_new2(buf);
b8524a
+    name = rb_str_new(0, len);
b8524a
+    while (gethostname(RSTRING_PTR(name), len) < 0) {
b8524a
+	int e = errno;
b8524a
+	switch (e) {
b8524a
+	  case ENAMETOOLONG:
b8524a
+#ifdef __linux__
b8524a
+	  case EINVAL:
b8524a
+	    /* glibc before version 2.1 uses EINVAL instead of ENAMETOOLONG */
b8524a
+#endif
b8524a
+	    break;
b8524a
+	  default:
b8524a
+	    rb_syserr_fail(e, "gethostname(3)");
b8524a
+	}
b8524a
+	rb_str_modify_expand(name, len);
b8524a
+	len += len;
b8524a
+    }
b8524a
+    rb_str_resize(name, strlen(RSTRING_PTR(name)));
b8524a
+    return name;
b8524a
 }
b8524a
 #else
b8524a
 #ifdef HAVE_UNAME