From 1edb13125ff62d4b272292dc656d60a0c82fb427 Mon Sep 17 00:00:00 2001 From: Gabriele Renzi Date: Tue, 17 Apr 2012 18:11:57 +0200 Subject: [PATCH] make psych less noisy by checking if a value is a valid YAML !!int or ruby !!float before attempting to convert it --- ext/psych/lib/psych/scalar_scanner.rb | 28 +++++++++++++++++++++------- test/psych/test_numeric.rb | 17 +++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb index fa2d385..cce542d 100644 --- a/ext/psych/lib/psych/scalar_scanner.rb +++ b/ext/psych/lib/psych/scalar_scanner.rb @@ -13,6 +13,16 @@ module Psych |[-+]?\.(inf|Inf|INF)(?# infinity) |\.(nan|NaN|NAN)(?# not a number))$/x + # Taken from http://yaml.org/type/int.html but accepts "," as a separator + # as this is the current psych behaviour + INT = /^(?: + [-+]?0b[0-1_,]+ # base 2 + |[-+]?0[0-7_,]+ # base 8 + |[-+]? (?: 0|[1-9][0-9_,]* ) # base 10 + |[-+]?0x[0-9a-fA-F_,]+ # base 16 + |[-+]?[1-9][0-9_,]* (?: :[0-5]?[0-9] )+ # base 60 + )$/x + # Create a new scanner def initialize @string_cache = {} @@ -78,20 +88,24 @@ module Psych end i when FLOAT + if string.count('.') < 2 + begin + return Float(string.gsub(/[,_]/, '')) + rescue ArgumentError + end + end + + @string_cache[string] = true + string + when INT begin - return Float(string.gsub(/[,_]/, '')) + return Integer(string.gsub(/[,_]/, '')) rescue ArgumentError end @string_cache[string] = true string else - if string.count('.') < 2 - begin - return Integer(string.gsub(/[,_]/, '')) - rescue ArgumentError - end - end @string_cache[string] = true string diff --git a/test/psych/test_numeric.rb b/test/psych/test_numeric.rb index bae723a..53a8d28 100644 --- a/test/psych/test_numeric.rb +++ b/test/psych/test_numeric.rb @@ -7,6 +7,16 @@ module Psych # 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 +31,12 @@ module Psych 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 -- 1.7.7