`Socket#unpack_sockaddr_un': sockaddr_un size differs - 15 r


#1

e$B$U$H5$$,$D$$$?$N$G$9$,!“e(BSocket.unpack_sockaddr_un e$B$Ke(B
UNIXServer#getsockname e$B$GF@$?%”%I%l%9$rEO$9$H%(%i!<$K$J$j$^e(B
e$B$9!#e(B

% ./ruby -v -rsocket -e ’
File.unlink("/tmp/tmpsock") rescue nil
s = UNIXServer.new("/tmp/tmpsock")
p s.getsockname
p Socket.unpack_sockaddr_un(s.getsockname)’
ruby 1.9.0 (2006-06-01) [i686-linux]
“\001\000/tmp/tmpsock\000”
-e:5:in `Socket#unpack_sockaddr_un’: sockaddr_un size differs - 15
required; 110 given (TypeError)
from -e:5

getsockname e$B$GF@$?$b$N$r$&$^$/07$($J$$$N$OJQ$8$c$J$$$G$7$g$&e(B
e$B$+!#e(B


#2

In article removed_email_address@domain.invalid,
Tanaka A. removed_email_address@domain.invalid writes:

“\001\000/tmp/tmpsock\000”
-e:5:in `Socket#unpack_sockaddr_un’: sockaddr_un size differs - 15 required; 110 given (TypeError)
from -e:5

getsockname e$B$GF@$?$b$N$r$&$^$/07$($J$$$N$OJQ$8$c$J$$$G$7$g$&e(B
e$B$+!#e(B

e$B$3$NB>!"L5L>$Ne(B UNIX e$B%=%1%C%H$Ne(B addr, peeraddr, path
e$B$,JQ$@$H$+!"e(B

% ruby -v -rsocket -e ’
s1, s2 = UNIXSocket.pair
p s1.addr
p s1.peeraddr
p s1.path

ruby 1.9.0 (2006-06-02) [i686-linux]
[“AF_UNIX”, “\224\272\332\267\2408\356\267\241j\341\267HZ\364\267”]
[“AF_UNIX”, “\374 \332\267\034)\332\267\200v\024\010\034)\332\267\001”]
“\034)\332\267\200v\024\010\034)\332\267\001”

UNIXSocket#path e$B$,@\B3@h$N%Q%9$rJV$9$H$+!"e(B

% ruby -v -rsocket -e ’
UNIXServer.open("/tmp/s") {|serv|
p UNIXSocket.open("/tmp/s").path
}’
ruby 1.9.0 (2006-06-02) [i686-linux]
“/tmp/s”

UNIXServer.open e$B$G:n$C$?$O$:$N$H$3$m$K%=%1%C%H$,$J$$$H$+!"e(B

% ruby -v -rsocket -e ’
path = “/tmp/” + “s” * 120
UNIXServer.open(path) {|serv|
p File.stat(path)
}’
ruby 1.9.0 (2006-06-02) [i686-linux]
-e:4:in `File#stat’: No such file or directory -
/tmp/ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
(Errno::ENOENT)
from -e:4
from -e:3

e$B$H$$$C$?LdBj$rD>$9$K$O$3$&$G$9$+$M!#e(B

Index: ext/socket/socket.c

RCS file: /src/ruby/ext/socket/socket.c,v
retrieving revision 1.167
diff -u -p -r1.167 socket.c
— ext/socket/socket.c 2 Jun 2006 12:06:09 -0000 1.167
+++ ext/socket/socket.c 3 Jun 2006 04:38:24 -0000
@@ -543,7 +543,7 @@ bsock_do_not_reverse_lookup_set(sock, st

static VALUE ipaddr _((struct sockaddr*, int));
#ifdef HAVE_SYS_UN_H
-static VALUE unixaddr _((struct sockaddr_un*));
+static VALUE unixaddr _((struct sockaddr_un*, socklen_t));
#endif

enum sock_recv_type {
@@ -620,10 +620,7 @@ s_recvfrom(sock, argc, argv, from)

#ifdef HAVE_SYS_UN_H
case RECV_UNIX:

  •    if (alen) /* connection-oriented socket may not return a from 
    

result */

  •        return rb_assoc_new(str, unixaddr((struct 
    

sockaddr_un*)buf));

  •    else
    
  •        return rb_assoc_new(str, Qnil);
    
  •    return rb_assoc_new(str, unixaddr((struct sockaddr_un*)buf, 
    

alen));
#endif
case RECV_SOCKET:
return rb_assoc_new(str, rb_str_new(buf, alen));
@@ -685,8 +682,7 @@ s_recvfrom_nonblock(int argc, VALUE *arg

#ifdef HAVE_SYS_UN_H
case RECV_UNIX:

  •    if (alen) /* connection-oriented socket may not return a from 
    

result */

  •        addr = unixaddr((struct sockaddr_un*)buf);
    
  •    addr = unixaddr((struct sockaddr_un*)buf, alen);
       break;
    

#endif

@@ -1607,8 +1603,11 @@ init_unixsock(sock, path, server)

 MEMZERO(&sockaddr, struct sockaddr_un, 1);
 sockaddr.sun_family = AF_UNIX;
  • strncpy(sockaddr.sun_path, RSTRING(path)->ptr,
    sizeof(sockaddr.sun_path)-1);
  • sockaddr.sun_path[sizeof(sockaddr.sun_path)-1] = ‘\0’;
  • if (sizeof(sockaddr.sun_path) <= RSTRING(path)->len) {
  •    rb_raise(rb_eArgError, "too long unix socket path (max: 
    

%dbytes)",

  •        (int)sizeof(sockaddr.sun_path)-1);
    
  • }

  • strcpy(sockaddr.sun_path, StringValueCStr(path));

    if (server) {
    status = bind(fd, (struct sockaddr*)&sockaddr,
    sizeof(sockaddr));
    @@ -1634,7 +1633,9 @@ init_unixsock(sock, path, server)

    init_sock(sock, fd);
    GetOpenFile(sock, fptr);

  • fptr->path = strdup(RSTRING(path)->ptr);
  • if (server) {

  •    fptr->path = strdup(RSTRING(path)->ptr);
    
  • }

    return sock;
    }
    @@ -1866,6 +1867,15 @@ unix_init(sock, path)
    return init_unixsock(sock, path, 0);
    }

+static char *
+unixpath(struct sockaddr_un *sockaddr, socklen_t len)
+{

  • if (sockaddr->sun_path < (char*)sockaddr + len)
  •    return sockaddr->sun_path;
    
  • else
  •    return "";
    

+}
+
static VALUE
unix_path(sock)
VALUE sock;
@@ -1878,7 +1888,7 @@ unix_path(sock)
socklen_t len = sizeof(addr);
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail(0);

  • fptr->path = strdup(addr.sun_path);
  • fptr->path = strdup(unixpath(&addr, len));
    }
    return rb_str_new2(fptr->path);
    }
    @@ -2201,11 +2211,12 @@ unix_sysaccept(sock)

#ifdef HAVE_SYS_UN_H
static VALUE
-unixaddr(sockaddr)
+unixaddr(sockaddr, len)
struct sockaddr_un *sockaddr;

  • socklen_t len;
    {
    return rb_assoc_new(rb_str_new2(“AF_UNIX”),
  •   	rb_str_new2(sockaddr->sun_path));
    
  •                    rb_str_new2(unixpath(sockaddr, len)));
    

}
#endif

@@ -2221,9 +2232,7 @@ unix_addr(sock)

 if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail("getsockname(2)");
  • if (len == 0)
  •    addr.sun_path[0] = '\0';
    
  • return unixaddr(&addr);
  • return unixaddr(&addr, len);
    }

static VALUE
@@ -2238,9 +2247,7 @@ unix_peeraddr(sock)

 if (getpeername(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail("getsockname(2)");
  • if (len == 0)
  •    addr.sun_path[0] = '\0';
    
  • return unixaddr(&addr);
  • return unixaddr(&addr, len);
    }
    #endif

@@ -3516,11 +3523,17 @@ sock_s_pack_sockaddr_un(self, path)
VALUE self, path;
{
struct sockaddr_un sockaddr;

  • char *sun_path;
    VALUE addr;

    MEMZERO(&sockaddr, struct sockaddr_un, 1);
    sockaddr.sun_family = AF_UNIX;

  • strncpy(sockaddr.sun_path, StringValuePtr(path),
    sizeof(sockaddr.sun_path)-1);
  • sun_path = StringValueCStr(path);
  • if (sizeof(sockaddr.sun_path) <= strlen(sun_path)) {
  •    rb_raise(rb_eArgError, "too long unix socket path (max: 
    

%dbytes)",

  •        (int)sizeof(sockaddr.sun_path)-1);
    
  • }
  • strncpy(sockaddr.sun_path, sun_path, sizeof(sockaddr.sun_path)-1);
    addr = rb_str_new((char*)&sockaddr, sizeof(sockaddr));
    OBJ_INFECT(addr, path);

@@ -3532,15 +3545,21 @@ sock_s_unpack_sockaddr_un(self, addr)
VALUE self, addr;
{
struct sockaddr_un * sockaddr;

  • char *sun_path;
    VALUE path;

    sockaddr = (struct sockaddr_un*)StringValuePtr(addr);

  • if (RSTRING(addr)->len != sizeof(struct sockaddr_un)) {
  • rb_raise(rb_eTypeError, “sockaddr_un size differs - %ld required; %d
    given”,
  • if (sizeof(struct sockaddr_un) < RSTRING(addr)->len) {
  • rb_raise(rb_eTypeError, “too long sockaddr_un - %ld longer than %d”,
    RSTRING(addr)->len, sizeof(struct sockaddr_un));
    }
  • /* xxx: should I check against sun_path size? */
  • path = rb_str_new2(sockaddr->sun_path);
  • sun_path = unixpath(sockaddr, RSTRING(addr)->len);
  • if (sizeof(struct sockaddr_un) == RSTRING(addr)->len &&
  •    sun_path == sockaddr->sun_path &&
    
  •    sun_path + strlen(sun_path) == RSTRING(addr)->ptr + 
    

RSTRING(addr)->len) {

  •    rb_raise(rb_eArgError, "sockaddr_un.sun_path not NUL 
    

terminated");

  • }
  • path = rb_str_new2(sun_path);
    OBJ_INFECT(path, addr);
    return path;
    }

#3

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:28683] Re: `Socket#unpack_sockaddr_un’:
sockaddr_un size differs - 15 required; 110 given (TypeError)”
on Sat, 3 Jun 2006 13:49:29 +0900, Tanaka A. removed_email_address@domain.invalid
writes:

|In article removed_email_address@domain.invalid,
| Tanaka A. removed_email_address@domain.invalid writes:
|
|> e$B$U$H5$$,$D$$$?$N$G$9$,!“e(BSocket.unpack_sockaddr_un e$B$Ke(B
|> UNIXServer#getsockname e$B$GF@$?%”%I%l%9$rEO$9$H%(%i!<$K$J$j$^e(B
|> e$B$9!#e(B

|> getsockname e$B$GF@$?$b$N$r$&$^$/07$($J$$$N$OJQ$8$c$J$$$G$7$g$&e(B
|> e$B$+!#e(B
|
|e$B$3$NB>!"L5L>$Ne(B UNIX e$B%=%1%C%H$Ne(B addr, peeraddr, path e$B$,JQ$@$H$+!"e(B
|UNIXSocket#path e$B$,@\B3@h$N%Q%9$rJV$9$H$+!"e(B
|UNIXServer.open e$B$G:n$C$?$O$:$N$H$3$m$K%=%1%C%H$,$J$$$H$+!"e(B
|e$B$H$$$C$?LdBj$rD>$9$K$O$3$&$G$9$+$M!#e(B

e$B%3%_%C%H$7$F$$$?$@$1$^$;$s$+!)e(B