diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-01-28 08:32:44 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-01-28 08:32:44 +0000 |
commit | d567ac3d1d41f5d3ee4b19268a7c6c2ca628e7c1 (patch) | |
tree | da0fc822fc7f8c8500ee612c84574db11ffd2824 /ext/socket/socket.c | |
parent | ee672da1b8f309db014b1acc9df45e783e4c9d98 (diff) | |
download | ruby-d567ac3d1d41f5d3ee4b19268a7c6c2ca628e7c1.tar.gz |
socket.c: unlimited size hostname
* ext/socket/socket.c (sock_gethostname): support unlimited size
hostname.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53677 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/socket.c')
-rw-r--r-- | ext/socket/socket.c | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/ext/socket/socket.c b/ext/socket/socket.c index dcf2498daa..542cd02ed6 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -893,13 +893,27 @@ 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; - 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 |