Project

General

Profile

Actions

Bug #12799

closed

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

Added by hsbt (Hiroshi SHIBATA) about 8 years ago. Updated about 8 years ago.

Status:
Closed
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[ruby-core:77365] Updated by Akinori MUSHA 5 days ago

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


#2[ruby-core:77387] 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

Actions #1

Updated by knu (Akinori MUSHA) about 8 years 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. [ruby-core:77451]
    [Bug #12799]

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

Updated by knu (Akinori MUSHA) about 8 years ago

Committed, thanks!

Actions

Also available in: Atom PDF

Like0
Like0Like0