summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/socket/basicsocket.c8
-rw-r--r--ext/socket/init.c4
-rw-r--r--ext/socket/unixsocket.c6
3 files changed, 18 insertions, 0 deletions
diff --git a/ext/socket/basicsocket.c b/ext/socket/basicsocket.c
index c1ae88d80cd..2ef4f32692a 100644
--- a/ext/socket/basicsocket.c
+++ b/ext/socket/basicsocket.c
@@ -358,11 +358,13 @@ bsock_getsockname(VALUE sock)
{
struct sockaddr_storage buf;
socklen_t len = (socklen_t)sizeof buf;
+ socklen_t len0 = len;
rb_io_t *fptr;
GetOpenFile(sock, fptr);
if (getsockname(fptr->fd, (struct sockaddr*)&buf, &len) < 0)
rb_sys_fail("getsockname(2)");
+ if (len0 < len) len = len0;
return rb_str_new((char*)&buf, len);
}
@@ -387,11 +389,13 @@ bsock_getpeername(VALUE sock)
{
struct sockaddr_storage buf;
socklen_t len = (socklen_t)sizeof buf;
+ socklen_t len0 = len;
rb_io_t *fptr;
GetOpenFile(sock, fptr);
if (getpeername(fptr->fd, (struct sockaddr*)&buf, &len) < 0)
rb_sys_fail("getpeername(2)");
+ if (len0 < len) len = len0;
return rb_str_new((char*)&buf, len);
}
@@ -475,11 +479,13 @@ bsock_local_address(VALUE sock)
{
struct sockaddr_storage buf;
socklen_t len = (socklen_t)sizeof buf;
+ socklen_t len0 = len;
rb_io_t *fptr;
GetOpenFile(sock, fptr);
if (getsockname(fptr->fd, (struct sockaddr*)&buf, &len) < 0)
rb_sys_fail("getsockname(2)");
+ if (len0 < len) len = len0;
return rsock_fd_socket_addrinfo(fptr->fd, (struct sockaddr *)&buf, len);
}
@@ -507,11 +513,13 @@ bsock_remote_address(VALUE sock)
{
struct sockaddr_storage buf;
socklen_t len = (socklen_t)sizeof buf;
+ socklen_t len0 = len;
rb_io_t *fptr;
GetOpenFile(sock, fptr);
if (getpeername(fptr->fd, (struct sockaddr*)&buf, &len) < 0)
rb_sys_fail("getpeername(2)");
+ if (len0 < len) len = len0;
return rsock_fd_socket_addrinfo(fptr->fd, (struct sockaddr *)&buf, len);
}
diff --git a/ext/socket/init.c b/ext/socket/init.c
index 696b62d82ac..43d21cc42f0 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -481,6 +481,8 @@ static int
cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len)
{
int ret;
+ socklen_t len0 = 0;
+ if (address_len) len0 = *address_len;
#ifdef HAVE_ACCEPT4
static int try_accept4 = 1;
if (try_accept4) {
@@ -489,6 +491,7 @@ cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len)
if (ret != -1) {
if (ret <= 2)
rb_maygvl_fd_fix_cloexec(ret);
+ if (address_len && len0 < *address_len) *address_len = len0;
return ret;
}
if (errno == ENOSYS) {
@@ -503,6 +506,7 @@ cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len)
ret = accept(socket, address, address_len);
#endif
if (ret == -1) return -1;
+ if (address_len && len0 < *address_len) *address_len = len0;
rb_maygvl_fd_fix_cloexec(ret);
return ret;
}
diff --git a/ext/socket/unixsocket.c b/ext/socket/unixsocket.c
index 84dcacd98db..75da9c1a78e 100644
--- a/ext/socket/unixsocket.c
+++ b/ext/socket/unixsocket.c
@@ -120,8 +120,10 @@ unix_path(VALUE sock)
if (NIL_P(fptr->pathv)) {
struct sockaddr_un addr;
socklen_t len = (socklen_t)sizeof(addr);
+ socklen_t len0 = len;
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail(0);
+ if (len0 < len) len = len0;
fptr->pathv = rb_obj_freeze(rsock_unixpath_str(&addr, len));
}
return rb_str_dup(fptr->pathv);
@@ -424,11 +426,13 @@ unix_addr(VALUE sock)
rb_io_t *fptr;
struct sockaddr_un addr;
socklen_t len = (socklen_t)sizeof addr;
+ socklen_t len0 = len;
GetOpenFile(sock, fptr);
if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail("getsockname(2)");
+ if (len0 < len) len = len0;
return rsock_unixaddr(&addr, len);
}
@@ -450,11 +454,13 @@ unix_peeraddr(VALUE sock)
rb_io_t *fptr;
struct sockaddr_un addr;
socklen_t len = (socklen_t)sizeof addr;
+ socklen_t len0 = len;
GetOpenFile(sock, fptr);
if (getpeername(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
rb_sys_fail("getpeername(2)");
+ if (len0 < len) len = len0;
return rsock_unixaddr(&addr, len);
}