summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authornagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-12 14:06:21 +0000
committernagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-12 14:06:21 +0000
commitf1306d429cf34cb42d69cf10c6d6ed32af11e517 (patch)
tree0572a52b44e94e1447d32abfbabc9a4c18a7373c /lib
parent5e645629f3c4dc16931f0c1635565f875b02e12c (diff)
merge revision(s) 59312: [Backport #13739]
optparse.rb: get rid of eval * lib/optparse.rb: try Float() and Integer() instead of eval, which does too much things. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@59316 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/optparse.rb20
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb
index d887e2f98d..eb502679c4 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1842,7 +1842,7 @@ XXX
#
# Float number format, and converts to Float.
#
- float = "(?:#{decimal}(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?"
+ float = "(?:#{decimal}(?=(.)?)(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?"
floatpat = %r"\A[-+]?#{float}\z"io
accept(Float, floatpat) {|s,| s.to_f if s}
@@ -1851,11 +1851,13 @@ XXX
# for float format, and Rational for rational format.
#
real = "[-+]?(?:#{octal}|#{float})"
- accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, n|
+ accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, f, n,|
if n
Rational(d, n)
- elsif s
- eval(s)
+ elsif f
+ Float(s)
+ else
+ Integer(s)
end
}
@@ -1889,10 +1891,14 @@ XXX
# integer format, Float for float format.
#
DecimalNumeric = floatpat # decimal integer is allowed as float also.
- accept(DecimalNumeric, floatpat) {|s,|
+ accept(DecimalNumeric, floatpat) {|s, f|
begin
- eval(s)
- rescue SyntaxError
+ if f
+ Float(s)
+ else
+ Integer(s)
+ end
+ rescue ArgumentError
raise OptionParser::InvalidArgument, s
end if s
}