[前][次][番号順一覧][スレッド一覧]

ruby-changes:68367

From: Jeremy <ko1@a...>
Date: Mon, 11 Oct 2021 13:57:06 +0900 (JST)
Subject: [ruby-changes:68367] 9a321dd9b2 (master): [ruby/ipaddr] Make IPAddr#include? consider range of argument

https://git.ruby-lang.org/ruby.git/commit/?id=9a321dd9b2

From 9a321dd9b2fb929873a6b50b41efdf3bd3119536 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Thu, 31 Oct 2019 10:06:13 -0700
Subject: [ruby/ipaddr] Make IPAddr#include? consider range of argument

It would be nice to use Range#cover? here, but it doesn't work
correctly before Ruby 2.6. Switch to manual checks of the beginning
of end of the ranges.

Fixes Ruby Bug 14119

https://github.com/ruby/ipaddr/commit/f45630da31
---
 lib/ipaddr.rb       | 31 ++++++-------------------------
 test/test_ipaddr.rb |  2 ++
 2 files changed, 8 insertions(+), 25 deletions(-)

diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb
index 48141198ef..ccf9784dd7 100644
--- a/lib/ipaddr.rb
+++ b/lib/ipaddr.rb
@@ -167,34 +167,15 @@ class IPAddr https://github.com/ruby/ruby/blob/trunk/lib/ipaddr.rb#L167
   #   net1 = IPAddr.new("192.168.2.0/24")
   #   net2 = IPAddr.new("192.168.2.100")
   #   net3 = IPAddr.new("192.168.3.0")
+  #   net4 = IPAddr.new("192.168.2.0/16")
   #   p net1.include?(net2)     #=> true
   #   p net1.include?(net3)     #=> false
+  #   p net1.include?(net4)     #=> false
+  #   p net4.include?(net1)     #=> true
   def include?(other)
-    other = coerce_other(other)
-    if ipv4_mapped?
-      if (@mask_addr >> 32) != 0xffffffffffffffffffffffff
-        return false
-      end
-      mask_addr = (@mask_addr & IN4MASK)
-      addr = (@addr & IN4MASK)
-      family = Socket::AF_INET
-    else
-      mask_addr = @mask_addr
-      addr = @addr
-      family = @family
-    end
-    if other.ipv4_mapped?
-      other_addr = (other.to_i & IN4MASK)
-      other_family = Socket::AF_INET
-    else
-      other_addr = other.to_i
-      other_family = other.family
-    end
-
-    if family != other_family
-      return false
-    end
-    return ((addr & mask_addr) == (other_addr & mask_addr))
+    range = to_range
+    other = coerce_other(other).to_range
+    range.begin <= other.begin && range.end >= other.end
   end
   alias === include?
 
diff --git a/test/test_ipaddr.rb b/test/test_ipaddr.rb
index 029ad06642..2afe749db3 100644
--- a/test/test_ipaddr.rb
+++ b/test/test_ipaddr.rb
@@ -350,6 +350,8 @@ class TC_Operator < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_ipaddr.rb#L350
     assert_equal(true, net1.include?(IPAddr.new("192.168.2.0")))
     assert_equal(true, net1.include?(IPAddr.new("192.168.2.255")))
     assert_equal(false, net1.include?(IPAddr.new("192.168.3.0")))
+    assert_equal(true, net1.include?(IPAddr.new("192.168.2.0/28")))
+    assert_equal(false, net1.include?(IPAddr.new("192.168.2.0/16")))
     # test with integer parameter
     int = (192 << 24) + (168 << 16) + (2 << 8) + 13
 
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]