diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-06-14 10:11:01 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-06-14 10:11:01 +0000 |
commit | 2f8d3280e0bbbf6d964d84ab4ae9456a1e8e8eb0 (patch) | |
tree | 60ce42aa5876ecbcf9f07326d67172d06212dbc7 | |
parent | 11aa7c9604055b252c5d84c6b8cb5e95be7bfafa (diff) |
* ext/socket/raddrinfo.c (parse_numeric_port): Detect
port overflow.
(numeric_getaddrinfo): Use parse_numeric_port.
numeric_getaddrinfo fails if port is too big now.
This makes rb_getaddrinfo invokes the real getaddrinfo()
on such condition.
This change is related to [ruby-core:69355] [Bug #11179].
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50890 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | ext/socket/raddrinfo.c | 31 |
2 files changed, 39 insertions, 2 deletions
@@ -1,3 +1,13 @@ +Sun Jun 14 18:49:56 2015 Tanaka Akira <akr@fsij.org> + + * ext/socket/raddrinfo.c (parse_numeric_port): Detect + port overflow. + (numeric_getaddrinfo): Use parse_numeric_port. + numeric_getaddrinfo fails if port is too big now. + This makes rb_getaddrinfo invokes the real getaddrinfo() + on such condition. + This change is related to [ruby-core:69355] [Bug #11179]. + Sun Jun 14 17:26:03 2015 Tanaka Akira <akr@fsij.org> * enum.c (enum_chunk_while): New method Enumerable#chunk_while. diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index caa08fea47..370ea6907f 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -154,6 +154,32 @@ struct getaddrinfo_arg struct addrinfo **res; }; +static int +parse_numeric_port(const char *service, int *portp) +{ + unsigned long u; + + if (!service) { + *portp = 0; + return 1; + } + + if (strspn(service, "0123456789") != strlen(service)) + return 0; + + errno = 0; + u = STRTOUL(service, NULL, 10); + if (errno) + return 0; + + if (0x10000 <= u) + return 0; + + *portp = u; + + return 1; +} + static void * nogvl_getaddrinfo(void *arg) { @@ -181,7 +207,9 @@ numeric_getaddrinfo(const char *node, const char *service, # define inet_pton(f,s,d) rb_w32_inet_pton(f,s,d) # endif - if (node && (!service || strspn(service, "0123456789") == strlen(service))) { + int port; + + if (node && parse_numeric_port(service, &port)) { static const struct { int socktype; int protocol; @@ -191,7 +219,6 @@ numeric_getaddrinfo(const char *node, const char *service, { SOCK_RAW, 0 } }; struct addrinfo *ai = NULL; - int port = service ? (unsigned short)atoi(service): 0; int hint_family = hints ? hints->ai_family : PF_UNSPEC; int hint_socktype = hints ? hints->ai_socktype : 0; int hint_protocol = hints ? hints->ai_protocol : 0; |