Skip to content

Commit feb9114

Browse files
taketo1113Tietew
authored andcommitted
[ruby/ipaddr] Fix #ipv4_compat returning invalid prefix
ruby/ipaddr@7c85bb8dfd Co-authored-by: Tietew <[email protected]>
1 parent 4ac8b12 commit feb9114

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/ipaddr.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ def ipv4_compat
381381
if !ipv4?
382382
raise InvalidAddressError, "not an IPv4 address: #{@addr}"
383383
end
384-
return self.clone.set(@addr, Socket::AF_INET6)
384+
clone = self.clone.set(@addr, Socket::AF_INET6)
385+
clone.instance_variable_set(:@mask_addr, @mask_addr | 0xffffffffffffffffffffffff00000000)
386+
clone
385387
end
386388

387389
# Returns a new ipaddr built by converting the IPv6 address into a

test/test_ipaddr.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ def test_ipv4_compat
196196
}
197197
assert_equal("::192.168.1.2", b.to_s)
198198
assert_equal(Socket::AF_INET6, b.family)
199+
assert_equal(128, b.prefix)
200+
201+
a = IPAddr.new("192.168.0.0/16")
202+
b = a.ipv4_compat
203+
assert_equal("::192.168.0.0", b.to_s)
204+
assert_equal(Socket::AF_INET6, b.family)
205+
assert_equal(112, b.prefix)
199206
end
200207

201208
def test_ipv4_mapped

0 commit comments

Comments
 (0)