[前][次][番号順一覧][スレッド一覧]

ruby-changes:40016

From: nobu <ko1@a...>
Date: Sat, 10 Oct 2015 08:52:40 +0900 (JST)
Subject: [ruby-changes:40016] nobu:r52097 (trunk): udpsocket.c: memory leaks

nobu	2015-10-10 08:52:23 +0900 (Sat, 10 Oct 2015)

  New Revision: 52097

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52097

  Log:
    udpsocket.c: memory leaks
    
    * ext/socket/udpsocket.c (udp_connect, udp_bind, udp_send): fix
      memory leaks at closed socket.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/udpsocket.c
    trunk/test/socket/test_udp.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52096)
+++ ChangeLog	(revision 52097)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Oct 10 08:52:21 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/socket/udpsocket.c (udp_connect, udp_bind, udp_send): fix
+	  memory leaks at closed socket.
+
 Fri Oct  9 17:29:07 2015  Shugo Maeda  <shugo@r...>
 
 	* lib/net/ftp.rb (parse257): refactor.
Index: ext/socket/udpsocket.c
===================================================================
--- ext/socket/udpsocket.c	(revision 52096)
+++ ext/socket/udpsocket.c	(revision 52097)
@@ -84,8 +84,8 @@ udp_connect(VALUE sock, VALUE host, VALU https://github.com/ruby/ruby/blob/trunk/ext/socket/udpsocket.c#L84
     struct udp_arg arg;
     VALUE ret;
 
-    arg.res = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
     GetOpenFile(sock, fptr);
+    arg.res = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
     arg.fd = fptr->fd;
     ret = rb_ensure(udp_connect_internal, (VALUE)&arg,
 		    rsock_freeaddrinfo, (VALUE)arg.res);
@@ -112,8 +112,8 @@ udp_bind(VALUE sock, VALUE host, VALUE p https://github.com/ruby/ruby/blob/trunk/ext/socket/udpsocket.c#L112
     struct rb_addrinfo *res0;
     struct addrinfo *res;
 
-    res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
     GetOpenFile(sock, fptr);
+    res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
     for (res = res0->ai; res; res = res->ai_next) {
 	if (bind(fptr->fd, res->ai_addr, res->ai_addrlen) < 0) {
 	    continue;
@@ -166,8 +166,8 @@ udp_send(int argc, VALUE *argv, VALUE so https://github.com/ruby/ruby/blob/trunk/ext/socket/udpsocket.c#L166
     rb_scan_args(argc, argv, "4", &arg.mesg, &flags, &host, &port);
 
     StringValue(arg.mesg);
-    res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
     GetOpenFile(sock, fptr);
+    res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
     arg.fd = fptr->fd;
     arg.flags = NUM2INT(flags);
     for (res = res0->ai; res; res = res->ai_next) {
Index: test/socket/test_udp.rb
===================================================================
--- test/socket/test_udp.rb	(revision 52096)
+++ test/socket/test_udp.rb	(revision 52097)
@@ -69,4 +69,31 @@ class TestSocket_UDPSocket < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/socket/test_udp.rb#L69
   ensure
     u.close if u
   end
+
+  def test_bind_no_memory_leak
+    assert_no_memory_leak(["-rsocket"], <<-"end;", <<-"end;", rss: true)
+      s = UDPSocket.new
+      s.close
+    end;
+      100_000.times {begin s.bind("127.0.0.1", 1) rescue IOError; end}
+    end;
+  end
+
+  def test_connect_no_memory_leak
+    assert_no_memory_leak(["-rsocket"], <<-"end;", <<-"end;", rss: true)
+      s = UDPSocket.new
+      s.close
+    end;
+      100_000.times {begin s.connect("127.0.0.1", 1) rescue IOError; end}
+    end;
+  end
+
+  def test_send_no_memory_leak
+    assert_no_memory_leak(["-rsocket"], <<-"end;", <<-"end;", rss: true)
+      s = UDPSocket.new
+      s.close
+    end;
+      100_000.times {begin s.send("\0"*100, 0, "127.0.0.1", 1) rescue IOError; end}
+    end;
+  end
 end if defined?(UDPSocket)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]