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

ruby-changes:45252

From: nobu <ko1@a...>
Date: Sat, 14 Jan 2017 11:26:53 +0900 (JST)
Subject: [ruby-changes:45252] nobu:r57325 (trunk): Made #decode_rdata client to catch errors

nobu	2017-01-14 11:26:48 +0900 (Sat, 14 Jan 2017)

  New Revision: 57325

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57325

  Log:
    Made #decode_rdata client to catch errors
    
    * lib/resolv.rb (Resolv::DNS::Message::MessageDecoder#get_rr):
      re-raise an exception from decode_rdata as DecodeError, so it
      can report them to the top in more informative way.  It was not
      reflecting on errors of data and thus breaking.  Client code
      expects `DecodeError` and knows how to handle broken messages.
      [Fix GH-1511]

  Modified files:
    trunk/lib/resolv.rb
    trunk/test/resolv/test_dns.rb
Index: test/resolv/test_dns.rb
===================================================================
--- test/resolv/test_dns.rb	(revision 57324)
+++ test/resolv/test_dns.rb	(revision 57325)
@@ -205,6 +205,21 @@ class TestResolvDNS < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/resolv/test_dns.rb#L205
     assert_instance_of Resolv::IPv6, Resolv::IPv6.create('::1:127.0.0.1')
   end
 
+  def test_ipv6_should_be_16
+    ref = '[rubygems:1626]'
+
+    broken_message =
+      "\0\0\0\0\0\0\0\0\0\0\0\1" \
+      "\x03ns2\bdnsimple\x03com\x00" \
+      "\x00\x1C\x00\x01\x00\x02OD" \
+      "\x00\x10$\x00\xCB\x00 I\x00\x01\x00\x00\x00\x00"
+
+    e = assert_raise_with_message(Resolv::DNS::DecodeError, /IPv6 address must be 16 bytes/, ref) do
+      Resolv::DNS::Message.decode broken_message
+    end
+    assert_kind_of(ArgumentError, e.cause)
+  end
+
   def test_too_big_label_address
     n = 2000
     m = Resolv::DNS::Message::MessageEncoder.new {|msg|
Index: lib/resolv.rb
===================================================================
--- lib/resolv.rb	(revision 57324)
+++ lib/resolv.rb	(revision 57325)
@@ -1635,7 +1635,13 @@ class Resolv https://github.com/ruby/ruby/blob/trunk/lib/resolv.rb#L1635
           name = self.get_name
           type, klass, ttl = self.get_unpack('nnN')
           typeclass = Resource.get_class(type, klass)
-          res = self.get_length16 { typeclass.decode_rdata self }
+          res = self.get_length16 do
+            begin
+              typeclass.decode_rdata self
+            rescue => e
+              raise DecodeError, e.message, e.backtrace
+            end
+          end
           res.instance_variable_set :@ttl, ttl
           return name, ttl, res
         end

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

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