From f548ea1f4fa787030e2cbd206267a3baaa53114b Mon Sep 17 00:00:00 2001 From: nagachika Date: Wed, 9 Mar 2016 15:14:30 +0000 Subject: merge revision(s) 53677: [Backport #11877] * ext/socket/socket.c (sock_gethostname): support unlimited size hostname. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@54070 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/socket/socket.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'ext') diff --git a/ext/socket/socket.c b/ext/socket/socket.c index 81028c5afb..7a5eab55cf 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -1031,14 +1031,28 @@ sock_gethostname(VALUE obj) # define RUBY_MAX_HOST_NAME_LEN 1024 #endif - char buf[RUBY_MAX_HOST_NAME_LEN+1]; + long len = RUBY_MAX_HOST_NAME_LEN; + VALUE name; rb_secure(3); - if (gethostname(buf, (int)sizeof buf - 1) < 0) - rb_sys_fail("gethostname(3)"); - - buf[sizeof buf - 1] = '\0'; - return rb_str_new2(buf); + name = rb_str_new(0, len); + while (gethostname(RSTRING_PTR(name), len) < 0) { + int e = errno; + switch (e) { + case ENAMETOOLONG: +#ifdef __linux__ + case EINVAL: + /* glibc before version 2.1 uses EINVAL instead of ENAMETOOLONG */ +#endif + break; + default: + rb_syserr_fail(e, "gethostname(3)"); + } + rb_str_modify_expand(name, len); + len += len; + } + rb_str_resize(name, strlen(RSTRING_PTR(name))); + return name; } #else #ifdef HAVE_UNAME -- cgit v1.2.3