1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Hoisted out unixsocket_len, triming NUL chars from sun_path

This commit is contained in:
Nobuyoshi Nakada 2019-08-16 17:36:09 +09:00
parent 03f4a0b18e
commit 619f82bb6b
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60

View file

@ -597,16 +597,21 @@ rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
} }
#ifdef HAVE_SYS_UN_H #ifdef HAVE_SYS_UN_H
static long
unixsocket_len(const struct sockaddr_un *su, socklen_t socklen)
{
const char *s = su->sun_path, *e = (const char*)su + socklen;
while (s < e && *(e-1) == '\0')
e--;
return e - s;
}
VALUE VALUE
rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len) rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len)
{ {
char *s, *e; long n = unixsocket_len(sockaddr, len);
s = sockaddr->sun_path; if (n >= 0)
e = (char *)sockaddr + len; return rb_str_new(sockaddr->sun_path, n);
while (s < e && *(e-1) == '\0')
e--;
if (s <= e)
return rb_str_new(s, e-s);
else else
return rb_str_new2(""); return rb_str_new2("");
} }
@ -985,6 +990,12 @@ init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype)
init_addrinfo(rai, (struct sockaddr *)&un, len, init_addrinfo(rai, (struct sockaddr *)&un, len,
PF_UNIX, socktype, 0, Qnil, Qnil); 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 #endif
/* /*
@ -1232,16 +1243,15 @@ rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE r
{ {
struct sockaddr_un *addr = &sockaddr->un; struct sockaddr_un *addr = &sockaddr->un;
char *p, *s, *e; char *p, *s, *e;
long len = unixsocket_len(addr, socklen);
s = addr->sun_path; s = addr->sun_path;
e = (char*)addr + socklen; if (len < 0)
while (s < e && *(e-1) == '\0')
e--;
if (e < s)
rb_str_cat2(ret, "too-short-AF_UNIX-sockaddr"); 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"); rb_str_cat2(ret, "empty-path-AF_UNIX-sockaddr");
else { else {
int printable_only = 1; int printable_only = 1;
e = s + len;
p = s; p = s;
while (p < e) { while (p < e) {
printable_only = printable_only && ISPRINT(*p) && !ISSPACE(*p); printable_only = printable_only && ISPRINT(*p) && !ISSPACE(*p);
@ -1567,13 +1577,7 @@ addrinfo_mdump(VALUE self)
#ifdef HAVE_SYS_UN_H #ifdef HAVE_SYS_UN_H
case AF_UNIX: case AF_UNIX:
{ {
struct sockaddr_un *su = &rai->addr.un; sockaddr = rb_str_new(rai->addr.un.sun_path, rai_unixsocket_len(rai));
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);
break; break;
} }
#endif #endif
@ -2307,25 +2311,22 @@ addrinfo_unix_path(VALUE self)
rb_addrinfo_t *rai = get_addrinfo(self); rb_addrinfo_t *rai = get_addrinfo(self);
int family = ai_get_afamily(rai); int family = ai_get_afamily(rai);
struct sockaddr_un *addr; struct sockaddr_un *addr;
char *s, *e; long n;
if (family != AF_UNIX) if (family != AF_UNIX)
rb_raise(rb_eSocket, "need AF_UNIX address"); rb_raise(rb_eSocket, "need AF_UNIX address");
addr = &rai->addr.un; addr = &rai->addr.un;
s = addr->sun_path; n = rai_unixsocket_len(rai);
e = (char*)addr + rai->sockaddr_len; if (n < 0)
if (e < s)
rb_raise(rb_eSocket, "too short AF_UNIX address: %"PRIuSIZE" bytes given for minimum %"PRIuSIZE" bytes.", 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)); (size_t)rai->sockaddr_len, offsetof(struct sockaddr_un, sun_path));
if (addr->sun_path + sizeof(addr->sun_path) < e) if ((long)sizeof(addr->sun_path) < n)
rb_raise(rb_eSocket, rb_raise(rb_eSocket,
"too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)", "too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
(size_t)(e - addr->sun_path), sizeof(addr->sun_path)); (size_t)n, sizeof(addr->sun_path));
while (s < e && *(e-1) == '\0') return rb_str_new(addr->sun_path, n);
e--;
return rb_str_new(s, e-s);
} }
#endif #endif