ruby-changes:38965
From: eregon <ko1@a...>
Date: Sat, 27 Jun 2015 19:11:22 +0900 (JST)
Subject: [ruby-changes:38965] eregon:r51046 (trunk): * lib/net/ftp.rb (makeport): close the TCPServer
eregon 2015-06-27 19:10:54 +0900 (Sat, 27 Jun 2015) New Revision: 51046 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51046 Log: * lib/net/ftp.rb (makeport): close the TCPServer when sending the port fails. * test/net/ftp/test_ftp.rb: test for above. Modified files: trunk/ChangeLog trunk/lib/net/ftp.rb trunk/test/net/ftp/test_ftp.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 51045) +++ ChangeLog (revision 51046) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Jun 14 19:02:03 2015 Benoit Daloze <eregontp@g...> + + * lib/net/ftp.rb (makeport): close the TCPServer + when sending the port fails. + + * test/net/ftp/test_ftp.rb: test for above. + Fri Jun 26 12:48:37 2015 Nobuyoshi Nakada <nobu@r...> * transcode.c (load_transcoder_entry): fix transcoder loading race Index: lib/net/ftp.rb =================================================================== --- lib/net/ftp.rb (revision 51045) +++ lib/net/ftp.rb (revision 51046) @@ -377,15 +377,9 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L377 end private :sendport - # Constructs a TCPServer socket, and sends it the PORT command - # - # Returns the constructed TCPServer socket + # Constructs a TCPServer socket def makeport # :nodoc: - sock = TCPServer.open(@sock.addr[3], 0) - port = sock.addr[1] - host = sock.addr[3] - sendport(host, port) - return sock + TCPServer.open(@sock.addr[3], 0) end private :makeport @@ -421,6 +415,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L415 else sock = makeport begin + sendport(sock.addr[3], sock.addr[1]) if @resume and rest_offset resp = sendcmd("REST " + rest_offset.to_s) if resp[0] != ?3 Index: test/net/ftp/test_ftp.rb =================================================================== --- test/net/ftp/test_ftp.rb (revision 51045) +++ test/net/ftp/test_ftp.rb (revision 51046) @@ -469,6 +469,46 @@ class FTPTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/net/ftp/test_ftp.rb#L469 end end + def test_open_data_port_fail_no_leak + commands = [] + server = create_ftp_server { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("331 Please specify the password.\r\n") + commands.push(sock.gets) + sock.print("230 Login successful.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to ASCII mode.\r\n") + line = sock.gets + commands.push(line) + sock.print("421 Service not available, closing control connection.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + } + begin + begin + ftp = Net::FTP.new + ftp.read_timeout = 0.2 + ftp.connect(SERVER_ADDR, server.port) + ftp.login + assert_match(/\AUSER /, commands.shift) + assert_match(/\APASS /, commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + assert_raise(Net::FTPTempError){ ftp.list } + assert_equal("TYPE A\r\n", commands.shift) + assert_match(/\APORT /, commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + assert_equal(nil, commands.shift) + ensure + ftp.close if ftp + end + ensure + server.close + end + end + def test_retrbinary_read_timeout_exceeded commands = [] binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/