Created
July 24, 2014 23:49
-
-
Save haileys/a240362fe4925a9d9333 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git ext/socket/socket.c ext/socket/socket.c | |
index 6b145ac..38c79cd 100644 | |
--- ext/socket/socket.c | |
+++ ext/socket/socket.c | |
@@ -1023,17 +1023,25 @@ sock_sysaccept(VALUE sock) | |
static VALUE | |
sock_gethostname(VALUE obj) | |
{ | |
-#ifndef HOST_NAME_MAX | |
-# define HOST_NAME_MAX 1024 | |
-#endif | |
- char buf[HOST_NAME_MAX+1]; | |
+ size_t namelen = 64; | |
+ char* buf = malloc(namelen); | |
- rb_secure(3); | |
- if (gethostname(buf, (int)sizeof buf - 1) < 0) | |
+ while(1) { | |
+ int rc = gethostname(buf, namelen); | |
+ if (rc == 0) { | |
+ break; | |
+ } | |
+ if (errno == ENAMETOOLONG) { | |
+ namelen *= 2; | |
+ buf = realloc(buf, namelen); | |
+ continue; | |
+ } | |
rb_sys_fail("gethostname(3)"); | |
+ } | |
- buf[sizeof buf - 1] = '\0'; | |
- return rb_str_new2(buf); | |
+ VALUE str = rb_str_new(buf, namelen); | |
+ free(buf); | |
+ return str; | |
} | |
#else | |
#ifdef HAVE_UNAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment