ruby-changes:41604
From: nobu <ko1@a...>
Date: Thu, 28 Jan 2016 17:31:51 +0900 (JST)
Subject: [ruby-changes:41604] nobu:r53677 (trunk): socket.c: unlimited size hostname
nobu 2016-01-28 17:32:44 +0900 (Thu, 28 Jan 2016) New Revision: 53677 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53677 Log: socket.c: unlimited size hostname * ext/socket/socket.c (sock_gethostname): support unlimited size hostname. Modified files: trunk/ChangeLog trunk/ext/socket/socket.c Index: ChangeLog =================================================================== --- ChangeLog (revision 53676) +++ ChangeLog (revision 53677) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Jan 28 17:31:43 2016 Nobuyoshi Nakada <nobu@r...> + + * ext/socket/socket.c (sock_gethostname): support unlimited size + hostname. + Wed Jan 27 21:03:45 2016 SHIBATA Hiroshi <hsbt@r...> * test/-ext-/string/test_capacity.rb: Added missing library. Index: ext/socket/socket.c =================================================================== --- ext/socket/socket.c (revision 53676) +++ ext/socket/socket.c (revision 53677) @@ -893,13 +893,27 @@ sock_gethostname(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L893 # 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/