[前][次][番号順一覧][スレッド一覧]

ruby-changes:38809

From: akr <ko1@a...>
Date: Sun, 14 Jun 2015 19:11:27 +0900 (JST)
Subject: [ruby-changes:38809] akr:r50890 (trunk): * ext/socket/raddrinfo.c (parse_numeric_port): Detect

akr	2015-06-14 19:11:01 +0900 (Sun, 14 Jun 2015)

  New Revision: 50890

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50890

  Log:
    * 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].

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/raddrinfo.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50889)
+++ ChangeLog	(revision 50890)
@@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jun 14 18:49:56 2015  Tanaka Akira  <akr@f...>
+
+	* 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@f...>
 
 	* enum.c (enum_chunk_while): New method Enumerable#chunk_while.
Index: ext/socket/raddrinfo.c
===================================================================
--- ext/socket/raddrinfo.c	(revision 50889)
+++ ext/socket/raddrinfo.c	(revision 50890)
@@ -154,6 +154,32 @@ struct getaddrinfo_arg https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L154
     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, co https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L207
 #   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, co https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L219
 	    { 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;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]