Bug #12799
closedIPAddr#== throws an exception with objects that can't be converted to IPAddr
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