Issue #12779 has been updated by Akinori MUSHA.


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

----------------------------------------
Bug #12779: IPAddr#== throws an exception with objects that can't be converted to IPAddr
https://bugs.ruby-lang.org/issues/12779#change-60610

* Author: David Sexton
* Status: Open
* Priority: Normal
* Assignee: Akinori MUSHA
* ruby -v: 2.3.1
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
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.

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

~~~ ruby
"sometext"==IPAddr.new("1.1.1.1")
~~~
returns false.

This also causes other core functions to fail..

~~~ ruby
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

---Files--------------------------------
1435.diff (391 Bytes)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>