Project

General

Profile

Backport #1850

Net::FTP#login missing precondition. Little descriptive exception raised when given nil password together with a username different of anonymous

Added by miguelff (Miguel Fernández) about 10 years ago. Updated 2 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
[ruby-core:24645]

Description

=begin
When Net::FTP#login is invoked giving a not nil password, and a user different from "anonymous" the following error is raised: TypeError.new("can't convert nil into String")

This happens due to the fact that in lib/net/ftp.rb (line 381) password is concatenated with a string, without cheking first if it is nil.

The error can be reproduced this way:

[1] Simple (wrong) use of Net::FTP#login
========================================

ftp=Net::FTP.new "petra.euitio.uniovi.es"
ftp.login "i1650878"

Error stack trace
=================
TypeError: can't convert nil into String
from /usr/lib/ruby/1.8/net/ftp.rb:381:in +'
from /usr/lib/ruby/1.8/net/ftp.rb:381:in
login'
from /usr/lib/ruby/1.8/monitor.rb:242:in synchronize'
from /usr/lib/ruby/1.8/net/ftp.rb:378:in
login'
from (irb):7
from :0

Although the error is somehow easy to discover its meaning is not directly related with the use of Net::FTP#login method. What's more, if Net::FTP#login is used from a higher level API, it's quite difficult to figure out what's happening without getting deep into the code (See[2]). In my humble opinion, it shouldn't be Net::FTP users' concern if nil can't be concateneted with a String. I think this should be a contract that the own method has to provide by means of a precondition.

[2] - Not so wrong use of Net::FTP#login from Kernel#open in lib/open-uri.rb:
============================================================================
require 'open-uri'
open('ftp://i1891823@petra.euitio.uniovi.es/home/alumnos/i1891823/csv.txt')

Error stack trace
=================
TypeError: can't convert nil into String
from /usr/lib/ruby/1.8/net/ftp.rb:381:in +'
from /usr/lib/ruby/1.8/net/ftp.rb:381:in
login'
from /usr/lib/ruby/1.8/monitor.rb:242:in synchronize'
from /usr/lib/ruby/1.8/net/ftp.rb:378:in
login'
from /usr/lib/ruby/1.8/open-uri.rb:657:in buffer_open'
from /usr/lib/ruby/1.8/open-uri.rb:164:in
open_loop'
from /usr/lib/ruby/1.8/open-uri.rb:162:in catch'
from /usr/lib/ruby/1.8/open-uri.rb:162:in
open_loop'
from /usr/lib/ruby/1.8/open-uri.rb:132:in open_uri'
from /usr/lib/ruby/1.8/open-uri.rb:518:in
open'
from /usr/lib/ruby/1.8/open-uri.rb:30:in `open'
from (irb):14
=end

History

#1

Updated by jeremyevans0 (Jeremy Evans) 2 months ago

  • ruby -v deleted (ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux])
  • Status changed from Open to Closed
  • Description updated (diff)
  • Project changed from Ruby 1.8 to Backport187
  • Tracker changed from Bug to Backport

Also available in: Atom PDF