diff options
| author | Taketo Takashima <t.taketo1113@gmail.com> | 2025-09-27 01:11:39 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2025-09-26 23:25:25 +0000 |
| commit | feb9114a53ae1d1231bb9c405b1f443a74cf8ff4 (patch) | |
| tree | 5874a6a2b8431df3b6e0c3b840c54275e295230c | |
| parent | 4ac8b12a1acc814cdeb1fdd46048104bf0f19dde (diff) | |
[ruby/ipaddr] Fix `#ipv4_compat` returning invalid prefix
https://github.com/ruby/ipaddr/commit/7c85bb8dfd
Co-authored-by: Tietew <tietew@gmail.com>
| -rw-r--r-- | lib/ipaddr.rb | 4 | ||||
| -rw-r--r-- | test/test_ipaddr.rb | 7 |
2 files changed, 10 insertions, 1 deletions
diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index 525466bbd9..1ad7980e9e 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -381,7 +381,9 @@ class IPAddr if !ipv4? raise InvalidAddressError, "not an IPv4 address: #{@addr}" end - return self.clone.set(@addr, Socket::AF_INET6) + clone = self.clone.set(@addr, Socket::AF_INET6) + clone.instance_variable_set(:@mask_addr, @mask_addr | 0xffffffffffffffffffffffff00000000) + clone end # Returns a new ipaddr built by converting the IPv6 address into a diff --git a/test/test_ipaddr.rb b/test/test_ipaddr.rb index 7ecd37e9a4..ac8921e75c 100644 --- a/test/test_ipaddr.rb +++ b/test/test_ipaddr.rb @@ -196,6 +196,13 @@ class TC_IPAddr < Test::Unit::TestCase } assert_equal("::192.168.1.2", b.to_s) assert_equal(Socket::AF_INET6, b.family) + assert_equal(128, b.prefix) + + a = IPAddr.new("192.168.0.0/16") + b = a.ipv4_compat + assert_equal("::192.168.0.0", b.to_s) + assert_equal(Socket::AF_INET6, b.family) + assert_equal(112, b.prefix) end def test_ipv4_mapped |
