Project

General

Profile

Bug #12799

IPAddr#== throws an exception with objects that can't be converted to IPAddr

Added by hsbt (Hiroshi SHIBATA) about 1 year ago. Updated about 1 year ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:77451]

Description

I accidentally deleted issue #12779. It was created by https://bugs.ruby-lang.org/users/11301


Description

IPAddr#== uses IPAddr#coerce_other to attempt to convert the 'other' object into an IPAddr for comparison.
If this conversion fails, an exception is thrown, either 'IPAddr::InvalidAddressError' or a 'NoMethodError' referring to the lack of a 'to_i' function.

I would expect a call to == should only return a true or a false in any circumstances. If the tested object is not an IPAddr then clearly it should be false.

IPAddr.new("1.1.1.1")=="sometext"
raises IPAddr::InvalidAddressError: invalid address
"sometext"==IPAddr.new("1.1.1.1")

returns false.

This also causes other core functions to fail..

ip = IPAddr.new("1.1.1.1")
iprange = IPAddr.new("2.2.2.2") .. IPAddr.new("2.2.2.20")
arr = [ip, iprange]

arr.delete(iprange)

Results in NoMethodError: undefined method `to_i' for #Range:0x00000001901748

I have patched this at github https://github.com/ruby/ruby/pull/1435

and produced a diff file from the above pull request, apologies if I have done so incorrectly


#1 Updated by Akinori MUSHA 5 days ago

Good point. Would you mind adding a regression test to test/test_ipaddr.rb?


#2 Updated by David Sexton 4 days ago

Akinori MUSHA wrote:

Good point. Would you mind adding a regression test to test/test_ipaddr.rb?

No problem.

I have also noticed the same issue with <=> so have added a patch (and test) for that to return nil

Updates diff is at https://github.com/ruby/ruby/pull/1435.diff

Associated revisions

Revision 56594
Added by knu (Akinori MUSHA) about 1 year ago

IPAddr#== and IPAddr#<=> no longer raise an exception if coercion fails

  • lib/ipaddr.rb (IPAddr#==): If coercion fails, return false
    instead of passing through the exception.
    [Bug #12799]

  • lib/ipaddr.rb (IPAddr#<=>): If coercion fails, return nil
    instead of passing through the exception.
    [Bug #12799]

Revision 56594
Added by knu (Akinori MUSHA) about 1 year ago

IPAddr#== and IPAddr#<=> no longer raise an exception if coercion fails

  • lib/ipaddr.rb (IPAddr#==): If coercion fails, return false
    instead of passing through the exception.
    [Bug #12799]

  • lib/ipaddr.rb (IPAddr#<=>): If coercion fails, return nil
    instead of passing through the exception.
    [Bug #12799]

History

#1 Updated by knu (Akinori MUSHA) about 1 year ago

  • Status changed from Open to Closed

Applied in changeset r56594.


IPAddr#== and IPAddr#<=> no longer raise an exception if coercion fails

  • lib/ipaddr.rb (IPAddr#==): If coercion fails, return false
    instead of passing through the exception.
    [Bug #12799]

  • lib/ipaddr.rb (IPAddr#<=>): If coercion fails, return nil
    instead of passing through the exception.
    [Bug #12799]

#2 [ruby-core:77972] Updated by knu (Akinori MUSHA) about 1 year ago

Committed, thanks!

Also available in: Atom PDF