[ruby-core:77443] [Ruby trunk Bug#12779] IPAddr#== throws an exception with objects that can't be converted to IPAddr

From: cool1@...
Date: 2016-09-28 13:22:55 UTC
List: ruby-core #77443
Issue #12779 has been updated by aaron paul.

File mad.php added

David Sexton wrote:
> 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



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

* 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)
mad.php (98 KB)


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

In This Thread

Prev Next