Project

General

Profile

Bug #11260 ยป 0001-lib-net-ftp.rb-makeport-close-the-TCPServer.patch

Eregon (Benoit Daloze), 06/14/2015 10:06 AM

View differences:

ChangeLog
1
Sun Jun 14 19:02:03 2015  Benoit Daloze  <eregontp@gmail.com>
2

  
3
	* lib/net/ftp.rb (makeport): close the TCPServer
4
	  when sending the port fails.
5

  
6
	* test/net/ftp/test_ftp.rb: test for above.
7

  
1 8
Sun Jun 14 06:24:57 2015  Benoit Daloze  <eregontp@gmail.com>
2 9

  
3 10
	* test/ruby/test_io.rb: add test for IO.binread fd leak.
lib/net/ftp.rb
377 377
    end
378 378
    private :sendport
379 379

  
380
    # Constructs a TCPServer socket, and sends it the PORT command
381
    #
382
    # Returns the constructed TCPServer socket
380
    # Constructs a TCPServer socket
383 381
    def makeport # :nodoc:
384
      sock = TCPServer.open(@sock.addr[3], 0)
385
      port = sock.addr[1]
386
      host = sock.addr[3]
387
      sendport(host, port)
388
      return sock
382
      TCPServer.open(@sock.addr[3], 0)
389 383
    end
390 384
    private :makeport
391 385

  
......
421 415
      else
422 416
        sock = makeport
423 417
        begin
418
          sendport(sock.addr[3], sock.addr[1])
424 419
          if @resume and rest_offset
425 420
            resp = sendcmd("REST " + rest_offset.to_s)
426 421
            if resp[0] != ?3
test/net/ftp/test_ftp.rb
469 469
    end
470 470
  end
471 471

  
472
  def test_open_data_port_fail_no_leak
473
    commands = []
474
    server = create_ftp_server { |sock|
475
      sock.print("220 (test_ftp).\r\n")
476
      commands.push(sock.gets)
477
      sock.print("331 Please specify the password.\r\n")
478
      commands.push(sock.gets)
479
      sock.print("230 Login successful.\r\n")
480
      commands.push(sock.gets)
481
      sock.print("200 Switching to Binary mode.\r\n")
482
      commands.push(sock.gets)
483
      sock.print("200 Switching to ASCII mode.\r\n")
484
      line = sock.gets
485
      commands.push(line)
486
      sock.print("421 Service not available, closing control connection.\r\n")
487
      commands.push(sock.gets)
488
      sock.print("200 Switching to Binary mode.\r\n")
489
    }
490
    begin
491
      begin
492
        ftp = Net::FTP.new
493
        ftp.read_timeout = 0.2
494
        ftp.connect(SERVER_ADDR, server.port)
495
        ftp.login
496
        assert_match(/\AUSER /, commands.shift)
497
        assert_match(/\APASS /, commands.shift)
498
        assert_equal("TYPE I\r\n", commands.shift)
499
        assert_raise(Net::FTPTempError){ ftp.list }
500
        assert_equal("TYPE A\r\n", commands.shift)
501
        assert_match(/\APORT /, commands.shift)
502
        assert_equal("TYPE I\r\n", commands.shift)
503
        assert_equal(nil, commands.shift)
504
      ensure
505
        ftp.close if ftp
506
      end
507
    ensure
508
      server.close
509
    end
510
  end
511

  
472 512
  def test_retrbinary_read_timeout_exceeded
473 513
    commands = []
474 514
    binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
475
-