Skip to content

Commit

Permalink
Ipaddr#native must also coerce @mask_addr
Browse files Browse the repository at this point in the history
Before it would be left as an IPv6 mask causing `to_range` to fail.

```
>> IPAddr.new("::2").native.to_range
/opt/rubies/3.0.3/lib/ruby/3.0.0/ipaddr.rb:479:in `set': invalid address (IPAddr::InvalidAddressError)
```
  • Loading branch information
byroot committed Dec 2, 2021
1 parent 72a2649 commit f4d499d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ipaddr.rb
Expand Up @@ -499,6 +499,7 @@ def set(addr, *family)
if addr < 0 || addr > IN4MASK
raise InvalidAddressError, "invalid address: #{@addr}"
end
@mask_addr &= IN4MASK
when Socket::AF_INET6
if addr < 0 || addr > IN6MASK
raise InvalidAddressError, "invalid address: #{@addr}"
Expand Down
5 changes: 5 additions & 0 deletions test/test_ipaddr.rb
Expand Up @@ -360,6 +360,11 @@ def test_include?

end

def test_native_coerce_mask_addr
assert_equal(IPAddr.new("0.0.0.2/255.255.255.255"), IPAddr.new("::2").native)
assert_equal(IPAddr.new("0.0.0.2/255.255.255.255").to_range, IPAddr.new("::2").native.to_range)
end

def test_loopback?
assert_equal(true, IPAddr.new('127.0.0.1').loopback?)
assert_equal(true, IPAddr.new('127.127.1.1').loopback?)
Expand Down

0 comments on commit f4d499d

Please sign in to comment.