diff options
-rw-r--r-- | object.c | 26 | ||||
-rw-r--r-- | test/ruby/test_float.rb | 6 |
2 files changed, 31 insertions, 1 deletions
@@ -3273,7 +3273,13 @@ rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error) char *const init_e = buf + DBL_DIG * 4; char *e = init_e; char prev = 0; + int dot_seen = FALSE; + switch (*p) {case '+': case '-': prev = *n++ = *p++;} + if (*p == '0') { + prev = *n++ = '0'; + while (*++p == '0'); + } while (p < end && n < e) prev = *n++ = *p++; while (*p) { if (*p == '_') { @@ -3284,8 +3290,26 @@ rb_cstr_to_dbl_raise(const char *p, int badcheck, int raise, int *error) } } prev = *p++; - if (e == init_e && (*p == 'e' || *p == 'E')) { + if (e == init_e && (prev == 'e' || prev == 'E' || prev == 'p' || prev == 'P')) { e = buf + sizeof(buf) - 1; + *n++ = prev; + switch (*p) {case '+': case '-': prev = *n++ = *p++;} + if (*p == '0') { + prev = *n++ = '0'; + while (*++p == '0'); + } + continue; + } + else if (ISSPACE(prev)) { + while (ISSPACE(*p)) ++p; + if (*p) { + if (badcheck) goto bad; + break; + } + } + else if (prev == '.' ? dot_seen++ : !ISDIGIT(prev)) { + if (badcheck) goto bad; + break; } if (n < e) *n++ = prev; } diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index 5686fd4617..3453440694 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -165,6 +165,12 @@ class TestFloat < Test::Unit::TestCase end assert_equal(1.0e10, Float("1.0_"+"00000"*Float::DIG+"e10")) + + z = "0" * (Float::DIG * 4 + 10) + all_assertions_foreach("long invalid string", "1.0", "1.0e", "1.0e-", "1.0e+") do |n| + assert_raise(ArgumentError, n += z + "A") {Float(n)} + assert_raise(ArgumentError, n += z + ".0") {Float(n)} + end end def test_divmod |