mirror of
https://github.com/ruby/ruby.git
synced 2025-08-24 05:25:34 +02:00
merge revision(s) r45045,r45046,r45530: [Backport #9697]
* ext/socket/ipsocket.c (ip_s_getaddress): Don't access freed memory. * ext/socket: Wrap struct addrinfo by struct rb_addrinfo. * ext/socket/socket.c (sock_s_getnameinfo): Save errno for EAI_SYSTEM. Reported by Saravana kumar. [ruby-core:61820] [Bug #9697] Fixed by Heesob Park. [ruby-core:61868] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@46054 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a3d95003e1
commit
e60ec9551c
7 changed files with 123 additions and 80 deletions
|
@ -1039,7 +1039,7 @@ sock_gethostname(VALUE obj)
|
|||
#endif
|
||||
|
||||
static VALUE
|
||||
make_addrinfo(struct addrinfo *res0, int norevlookup)
|
||||
make_addrinfo(struct rb_addrinfo *res0, int norevlookup)
|
||||
{
|
||||
VALUE base, ary;
|
||||
struct addrinfo *res;
|
||||
|
@ -1048,7 +1048,7 @@ make_addrinfo(struct addrinfo *res0, int norevlookup)
|
|||
rb_raise(rb_eSocket, "host not found");
|
||||
}
|
||||
base = rb_ary_new();
|
||||
for (res = res0; res; res = res->ai_next) {
|
||||
for (res = res0->ai; res; res = res->ai_next) {
|
||||
ary = rsock_ipaddr(res->ai_addr, res->ai_addrlen, norevlookup);
|
||||
if (res->ai_canonname) {
|
||||
RARRAY_PTR(ary)[2] = rb_str_new2(res->ai_canonname);
|
||||
|
@ -1271,7 +1271,8 @@ static VALUE
|
|||
sock_s_getaddrinfo(int argc, VALUE *argv)
|
||||
{
|
||||
VALUE host, port, family, socktype, protocol, flags, ret, revlookup;
|
||||
struct addrinfo hints, *res;
|
||||
struct addrinfo hints;
|
||||
struct rb_addrinfo *res;
|
||||
int norevlookup;
|
||||
|
||||
rb_scan_args(argc, argv, "25", &host, &port, &family, &socktype, &protocol, &flags, &revlookup);
|
||||
|
@ -1294,7 +1295,7 @@ sock_s_getaddrinfo(int argc, VALUE *argv)
|
|||
res = rsock_getaddrinfo(host, port, &hints, 0);
|
||||
|
||||
ret = make_addrinfo(res, norevlookup);
|
||||
freeaddrinfo(res);
|
||||
rb_freeaddrinfo(res);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -1327,8 +1328,9 @@ sock_s_getnameinfo(int argc, VALUE *argv)
|
|||
char *hptr, *pptr;
|
||||
char hbuf[1024], pbuf[1024];
|
||||
int fl;
|
||||
struct addrinfo hints, *res = NULL, *r;
|
||||
int error;
|
||||
struct rb_addrinfo *res = NULL;
|
||||
struct addrinfo hints, *r;
|
||||
int error, saved_errno;
|
||||
union_sockaddr ss;
|
||||
struct sockaddr *sap;
|
||||
socklen_t salen;
|
||||
|
@ -1412,8 +1414,8 @@ sock_s_getnameinfo(int argc, VALUE *argv)
|
|||
hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af);
|
||||
error = rb_getaddrinfo(hptr, pptr, &hints, &res);
|
||||
if (error) goto error_exit_addr;
|
||||
sap = res->ai_addr;
|
||||
salen = res->ai_addrlen;
|
||||
sap = res->ai->ai_addr;
|
||||
salen = res->ai->ai_addrlen;
|
||||
}
|
||||
else {
|
||||
rb_raise(rb_eTypeError, "expecting String or Array");
|
||||
|
@ -1424,7 +1426,7 @@ sock_s_getnameinfo(int argc, VALUE *argv)
|
|||
pbuf, sizeof(pbuf), fl);
|
||||
if (error) goto error_exit_name;
|
||||
if (res) {
|
||||
for (r = res->ai_next; r; r = r->ai_next) {
|
||||
for (r = res->ai->ai_next; r; r = r->ai_next) {
|
||||
char hbuf2[1024], pbuf2[1024];
|
||||
|
||||
sap = r->ai_addr;
|
||||
|
@ -1433,20 +1435,24 @@ sock_s_getnameinfo(int argc, VALUE *argv)
|
|||
pbuf2, sizeof(pbuf2), fl);
|
||||
if (error) goto error_exit_name;
|
||||
if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) {
|
||||
freeaddrinfo(res);
|
||||
rb_freeaddrinfo(res);
|
||||
rb_raise(rb_eSocket, "sockaddr resolved to multiple nodename");
|
||||
}
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
rb_freeaddrinfo(res);
|
||||
}
|
||||
return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
|
||||
|
||||
error_exit_addr:
|
||||
if (res) freeaddrinfo(res);
|
||||
saved_errno = errno;
|
||||
if (res) rb_freeaddrinfo(res);
|
||||
errno = saved_errno;
|
||||
rsock_raise_socket_error("getaddrinfo", error);
|
||||
|
||||
error_exit_name:
|
||||
if (res) freeaddrinfo(res);
|
||||
saved_errno = errno;
|
||||
if (res) rb_freeaddrinfo(res);
|
||||
errno = saved_errno;
|
||||
rsock_raise_socket_error("getnameinfo", error);
|
||||
|
||||
UNREACHABLE;
|
||||
|
@ -1469,10 +1475,10 @@ sock_s_getnameinfo(int argc, VALUE *argv)
|
|||
static VALUE
|
||||
sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
|
||||
{
|
||||
struct addrinfo *res = rsock_addrinfo(host, port, 0, 0);
|
||||
VALUE addr = rb_str_new((char*)res->ai_addr, res->ai_addrlen);
|
||||
struct rb_addrinfo *res = rsock_addrinfo(host, port, 0, 0);
|
||||
VALUE addr = rb_str_new((char*)res->ai->ai_addr, res->ai->ai_addrlen);
|
||||
|
||||
freeaddrinfo(res);
|
||||
rb_freeaddrinfo(res);
|
||||
OBJ_INFECT(addr, port);
|
||||
OBJ_INFECT(addr, host);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue