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

ruby-changes:25640

From: tenderlove <ko1@a...>
Date: Sat, 17 Nov 2012 12:11:39 +0900 (JST)
Subject: [ruby-changes:25640] tenderlove:r37697 (trunk): * ext/psych/lib/psych/scalar_scanner.rb: avoid raising exceptions when

tenderlove	2012-11-17 12:11:22 +0900 (Sat, 17 Nov 2012)

  New Revision: 37697

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37697

  Log:
    * ext/psych/lib/psych/scalar_scanner.rb: avoid raising exceptions when
      parsing Floats and Integers. Thanks riffraff [ruby-core:44426]
    * test/psych/test_numeric.rb: associated test

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/lib/psych/scalar_scanner.rb
    trunk/test/psych/test_numeric.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37696)
+++ ChangeLog	(revision 37697)
@@ -1,3 +1,9 @@
+Sat Nov 17 12:03:41 2012  Aaron Patterson <aaron@t...>
+
+	* ext/psych/lib/psych/scalar_scanner.rb: avoid raising exceptions when
+	  parsing Floats and Integers. Thanks riffraff [ruby-core:44426]
+	* test/psych/test_numeric.rb: associated test
+
 Sat Nov 17 11:34:31 2012  Hiroshi Shirosaki  <h.shirosaki@g...>
 
 	* st.c (st_update): pass the key in st_table so that we can free
Index: ext/psych/lib/psych/scalar_scanner.rb
===================================================================
--- ext/psych/lib/psych/scalar_scanner.rb	(revision 37696)
+++ ext/psych/lib/psych/scalar_scanner.rb	(revision 37697)
@@ -8,11 +8,17 @@
     TIME = /^\d{4}-\d{1,2}-\d{1,2}([Tt]|\s+)\d{1,2}:\d\d:\d\d(\.\d*)?(\s*Z|[-+]\d{1,2}(:\d\d)?)?/
 
     # Taken from http://yaml.org/type/float.html
-    FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9.]*([eE][-+][0-9]+)?(?# base 10)
+    FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10)
               |[-+]?[0-9][0-9_,]*(:[0-5]?[0-9])+\.[0-9_]*(?# base 60)
               |[-+]?\.(inf|Inf|INF)(?# infinity)
               |\.(nan|NaN|NAN)(?# not a number))$/x
 
+    # Taken from http://yaml.org/type/int.html
+    INTEGER = /^(?:[-+]?0b[0-1_]+          (?# base 2)
+                  |[-+]?0[0-7_]+           (?# base 8)
+                  |[-+]?(?:0|[1-9][0-9_]*) (?# base 10)
+                  |[-+]?0x[0-9a-fA-F_]+    (?# base 16))$/x
+
     # Create a new scanner
     def initialize
       @string_cache = {}
@@ -86,20 +92,15 @@
         end
         i
       when FLOAT
-        begin
-          return Float(string.gsub(/[,_]/, ''))
-        rescue ArgumentError
+        if string == '.'
+          @string_cache[string] = true
+          string
+        else
+          Float(string.gsub(/[,_]/, ''))
         end
-
-        @string_cache[string] = true
-        string
       else
-        if string.count('.') < 2
-          begin
-            return Integer(string.gsub(/[,_]/, ''))
-          rescue ArgumentError
-          end
-        end
+        int = parse_int string.gsub(/[,_]/, '')
+        return int if int
 
         @string_cache[string] = true
         string
@@ -107,6 +108,13 @@
     end
 
     ###
+    # Parse and return an int from +string+
+    def parse_int string
+      return unless INTEGER === string
+      Integer(string)
+    end
+
+    ###
     # Parse and return a Time from +string+
     def parse_time string
       date, time = *(string.split(/[ tT]/, 2))
Index: test/psych/test_numeric.rb
===================================================================
--- test/psych/test_numeric.rb	(revision 37696)
+++ test/psych/test_numeric.rb	(revision 37697)
@@ -7,6 +7,15 @@
   # http://yaml.org/type/float.html
   # http://yaml.org/type/int.html
   class TestNumeric < TestCase
+    def setup
+      @old_debug = $DEBUG
+      $DEBUG = true
+    end
+
+    def teardown
+      $DEBUG = @old_debug
+    end
+
     def test_non_float_with_0
       str = Psych.load('--- 090')
       assert_equal '090', str
@@ -21,5 +30,12 @@
       decimal = BigDecimal("12.34")
       assert_cycle decimal
     end
+
+    def test_does_not_attempt_numeric
+      str = Psych.load('--- 4 roses')
+      assert_equal '4 roses', str
+      str = Psych.load('--- 1.1.1')
+      assert_equal '1.1.1', str
+    end
   end
 end

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

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