From 0893ee492c189e4d2c5badfd326a2e45049c89a5 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Mon, 9 Dec 2019 21:47:59 +0900 Subject: Fix that `String#to_f` accepts consecutive `_` as a numeric expression Consecutive `_` is not allowed as a numeric expression: 1_2__3 #=> SyntaxError Float("1_2__3") #=> ArgumentError Integer("1_2__3") #=> ArgumentError "1_2__3".to_i #=> 12 But `String#to_f` accept it, so I fixed the issue. Before this patch: "1_2__3".to_f #=> 123 After this patch: "1_2__3".to_f #=> 12 --- src/string.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src/string.c') diff --git a/src/string.c b/src/string.c index 61fbd4ded..f75679513 100644 --- a/src/string.c +++ b/src/string.c @@ -2522,15 +2522,10 @@ bad: while (p < end && n < e) prev = *n++ = *p++; while (*p) { if (*p == '_') { - /* remove underscores between digits */ - if (badcheck) { - if (n == buf || !ISDIGIT(prev)) goto bad; - ++p; - if (!ISDIGIT(*p)) goto bad; - } - else { - while (*++p == '_'); - continue; + /* remove an underscore between digits */ + if (n == buf || !ISDIGIT(prev) || (++p, !ISDIGIT(*p))) { + if (badcheck) goto bad; + break; } } prev = *p++; -- cgit v1.2.3