summaryrefslogtreecommitdiff
path: root/ext/socket/socket.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-04-26 03:34:12 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-04-26 03:34:12 +0000
commit1413510ed2d8175dcce7c2c14c02e720c8406429 (patch)
treee91eb8fdd906f1ad20e6c0b686229ffff85f3b3b /ext/socket/socket.c
parent2bc4268e7900808b6965176a975c9d392a1239fa (diff)
* ext/socket/raddrinfo.c (init_unix_addrinfo): support the longest
path in sockaddr_un. (inspect_sockaddr): ditto. (addrinfo_mdump): ditto. (addrinfo_mload): ditto. (rsock_unixpath_str): new function. (rsock_unixpath): removed. (rsock_unixaddr): use rsock_unixpath_str. * ext/socket/socket.c (sock_s_pack_sockaddr_un): support the longest path in sockaddr_un. (sock_s_unpack_sockaddr_un): ditto. (sock_s_gethostbyaddr): unused variable removed. * ext/socket/unixsocket.c (rsock_init_unixsock): support the longest path in sockaddr_un. * ext/socket/rubysocket.h (rsock_unixpath_str): declared. (rsock_unixpath): removed. * test/socket/test_unix.rb: comment out test_nul because abstract unix sockets may contain NULs. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35474 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/socket.c')
-rw-r--r--ext/socket/socket.c23
1 files changed, 7 insertions, 16 deletions
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index 383bc29..6c0ed2f 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -1003,13 +1003,12 @@ sock_s_gethostbyaddr(int argc, VALUE *argv)
{
VALUE addr, family;
struct hostent *h;
- struct sockaddr *sa;
char **pch;
VALUE ary, names;
int t = AF_INET;
rb_scan_args(argc, argv, "11", &addr, &family);
- sa = (struct sockaddr*)StringValuePtr(addr);
+ StringValue(addr);
if (!NIL_P(family)) {
t = rsock_family_arg(family);
}
@@ -1422,17 +1421,16 @@ static VALUE
sock_s_pack_sockaddr_un(VALUE self, VALUE path)
{
struct sockaddr_un sockaddr;
- char *sun_path;
VALUE addr;
+ StringValue(path);
MEMZERO(&sockaddr, struct sockaddr_un, 1);
sockaddr.sun_family = AF_UNIX;
- sun_path = StringValueCStr(path);
- if (sizeof(sockaddr.sun_path) <= strlen(sun_path)) {
- rb_raise(rb_eArgError, "too long unix socket path (%ldbytes given but %dbytes max)",
- RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path)-1);
+ if (sizeof(sockaddr.sun_path) <= RSTRING_LEN(path)) {
+ rb_raise(rb_eArgError, "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
+ (size_t)RSTRING_LEN(path), sizeof(sockaddr.sun_path));
}
- strncpy(sockaddr.sun_path, sun_path, sizeof(sockaddr.sun_path)-1);
+ memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
addr = rb_str_new((char*)&sockaddr, sizeof(sockaddr));
OBJ_INFECT(addr, path);
@@ -1455,7 +1453,6 @@ static VALUE
sock_s_unpack_sockaddr_un(VALUE self, VALUE addr)
{
struct sockaddr_un * sockaddr;
- const char *sun_path;
VALUE path;
sockaddr = (struct sockaddr_un*)SockAddrStringValuePtr(addr);
@@ -1471,13 +1468,7 @@ sock_s_unpack_sockaddr_un(VALUE self, VALUE addr)
rb_raise(rb_eTypeError, "too long sockaddr_un - %ld longer than %d",
RSTRING_LEN(addr), (int)sizeof(struct sockaddr_un));
}
- sun_path = rsock_unixpath(sockaddr, RSTRING_LENINT(addr));
- if (sizeof(struct sockaddr_un) == RSTRING_LEN(addr) &&
- sun_path == sockaddr->sun_path &&
- sun_path + strlen(sun_path) == RSTRING_PTR(addr) + RSTRING_LEN(addr)) {
- rb_raise(rb_eArgError, "sockaddr_un.sun_path not NUL terminated");
- }
- path = rb_str_new2(sun_path);
+ path = rsock_unixpath_str(sockaddr, RSTRING_LENINT(addr));
OBJ_INFECT(path, addr);
return path;
}