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

ruby-changes:57092

From: Nobuyoshi <ko1@a...>
Date: Fri, 16 Aug 2019 17:50:48 +0900 (JST)
Subject: [ruby-changes:57092] Nobuyoshi Nakada: 619f82bb6b (master): Hoisted out unixsocket_len, triming NUL chars from sun_path

https://git.ruby-lang.org/ruby.git/commit/?id=619f82bb6b

From 619f82bb6bd913d8dbb8ca5da15ca1fbb4508629 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Fri, 16 Aug 2019 17:36:09 +0900
Subject: Hoisted out unixsocket_len, triming NUL chars from sun_path


diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c
index 1054d04..b59cc49 100644
--- a/ext/socket/raddrinfo.c
+++ b/ext/socket/raddrinfo.c
@@ -597,16 +597,21 @@ rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L597
 }
 
 #ifdef HAVE_SYS_UN_H
-VALUE
-rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len)
+static long
+unixsocket_len(const struct sockaddr_un *su, socklen_t socklen)
 {
-    char *s, *e;
-    s = sockaddr->sun_path;
-    e = (char *)sockaddr + len;
+    const char *s = su->sun_path, *e = (const char*)su + socklen;
     while (s < e && *(e-1) == '\0')
         e--;
-    if (s <= e)
-        return rb_str_new(s, e-s);
+    return e - s;
+}
+
+VALUE
+rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len)
+{
+    long n = unixsocket_len(sockaddr, len);
+    if (n >= 0)
+        return rb_str_new(sockaddr->sun_path, n);
     else
         return rb_str_new2("");
 }
@@ -985,6 +990,12 @@ init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L990
     init_addrinfo(rai, (struct sockaddr *)&un, len,
 		  PF_UNIX, socktype, 0, Qnil, Qnil);
 }
+
+static long
+rai_unixsocket_len(const rb_addrinfo_t *rai)
+{
+    return unixsocket_len(&rai->addr.un, rai->sockaddr_len);
+}
 #endif
 
 /*
@@ -1232,16 +1243,15 @@ rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE r https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1243
           {
             struct sockaddr_un *addr = &sockaddr->un;
             char *p, *s, *e;
+            long len = unixsocket_len(addr, socklen);
             s = addr->sun_path;
-            e = (char*)addr + socklen;
-            while (s < e && *(e-1) == '\0')
-                e--;
-            if (e < s)
+            if (len < 0)
                 rb_str_cat2(ret, "too-short-AF_UNIX-sockaddr");
-            else if (s == e)
+            else if (len == 0)
                 rb_str_cat2(ret, "empty-path-AF_UNIX-sockaddr");
             else {
                 int printable_only = 1;
+                e = s + len;
                 p = s;
                 while (p < e) {
                     printable_only = printable_only && ISPRINT(*p) && !ISSPACE(*p);
@@ -1567,13 +1577,7 @@ addrinfo_mdump(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1577
 #ifdef HAVE_SYS_UN_H
       case AF_UNIX:
       {
-        struct sockaddr_un *su = &rai->addr.un;
-        char *s, *e;
-        s = su->sun_path;
-        e = (char*)su + rai->sockaddr_len;
-        while (s < e && *(e-1) == '\0')
-            e--;
-        sockaddr = rb_str_new(s, e-s);
+        sockaddr = rb_str_new(rai->addr.un.sun_path, rai_unixsocket_len(rai));
         break;
       }
 #endif
@@ -2307,25 +2311,22 @@ addrinfo_unix_path(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L2311
     rb_addrinfo_t *rai = get_addrinfo(self);
     int family = ai_get_afamily(rai);
     struct sockaddr_un *addr;
-    char *s, *e;
+    long n;
 
     if (family != AF_UNIX)
 	rb_raise(rb_eSocket, "need AF_UNIX address");
 
     addr = &rai->addr.un;
 
-    s = addr->sun_path;
-    e = (char*)addr + rai->sockaddr_len;
-    if (e < s)
+    n = rai_unixsocket_len(rai);
+    if (n < 0)
         rb_raise(rb_eSocket, "too short AF_UNIX address: %"PRIuSIZE" bytes given for minimum %"PRIuSIZE" bytes.",
-            (size_t)rai->sockaddr_len, (size_t)(s - (char *)addr));
-    if (addr->sun_path + sizeof(addr->sun_path) < e)
+                 (size_t)rai->sockaddr_len, offsetof(struct sockaddr_un, sun_path));
+    if ((long)sizeof(addr->sun_path) < n)
         rb_raise(rb_eSocket,
             "too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
-            (size_t)(e - addr->sun_path), sizeof(addr->sun_path));
-    while (s < e && *(e-1) == '\0')
-        e--;
-    return rb_str_new(s, e-s);
+            (size_t)n, sizeof(addr->sun_path));
+    return rb_str_new(addr->sun_path, n);
 }
 #endif
 
-- 
cgit v0.10.2


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

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