Skip to content

Commit af48519

Browse files
committed
Ipaddr#native must also coerce @mask_addr
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) ```
1 parent 72a2649 commit af48519

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/ipaddr.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ def set(addr, *family)
509509
@addr = addr
510510
if family[0]
511511
@family = family[0]
512+
if @family == Socket::AF_INET
513+
@mask_addr &= IN4MASK
514+
end
512515
end
513516
return self
514517
end

test/test_ipaddr.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ def test_include?
360360

361361
end
362362

363+
def test_native_coerce_mask_addr
364+
assert_equal(IPAddr.new("0.0.0.2/255.255.255.255"), IPAddr.new("::2").native)
365+
assert_equal(IPAddr.new("0.0.0.2/255.255.255.255").to_range, IPAddr.new("::2").native.to_range)
366+
end
367+
363368
def test_loopback?
364369
assert_equal(true, IPAddr.new('127.0.0.1').loopback?)
365370
assert_equal(true, IPAddr.new('127.127.1.1').loopback?)

0 commit comments

Comments
 (0)