From 2fb1d374393da45f4931cbbc7e573e37ca97e00a Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Sun, 12 Nov 2023 11:13:31 +0900 Subject: [ruby/prism] Reject invalid rational literals like `1e1r` on lexing Fix https://github.com/ruby/prism/pull/1586 https://github.com/ruby/prism/commit/b3bde866f2 --- prism/prism.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'prism') diff --git a/prism/prism.c b/prism/prism.c index f0a4df7e6b..4bac4d8c76 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -6315,7 +6315,7 @@ pm_strspn_hexadecimal_number_validate(pm_parser_t *parser, const uint8_t *string } static pm_token_type_t -lex_optional_float_suffix(pm_parser_t *parser) { +lex_optional_float_suffix(pm_parser_t *parser, bool* seen_e) { pm_token_type_t type = PM_TOKEN_INTEGER; // Here we're going to attempt to parse the optional decimal portion of a @@ -6336,6 +6336,7 @@ lex_optional_float_suffix(pm_parser_t *parser) { // float. If it's not there, it's okay and we'll just continue on. if (match(parser, 'e') || match(parser, 'E')) { (void) (match(parser, '+') || match(parser, '-')); + *seen_e = true; if (pm_char_is_decimal_digit(*parser->current.end)) { parser->current.end++; @@ -6351,8 +6352,9 @@ lex_optional_float_suffix(pm_parser_t *parser) { } static pm_token_type_t -lex_numeric_prefix(pm_parser_t *parser) { +lex_numeric_prefix(pm_parser_t *parser, bool* seen_e) { pm_token_type_t type = PM_TOKEN_INTEGER; + *seen_e = false; if (peek_offset(parser, -1) == '0') { switch (*parser->current.end) { @@ -6423,14 +6425,14 @@ lex_numeric_prefix(pm_parser_t *parser) { // 0.xxx is a float case '.': { - type = lex_optional_float_suffix(parser); + type = lex_optional_float_suffix(parser, seen_e); break; } // 0exxx is a float case 'e': case 'E': { - type = lex_optional_float_suffix(parser); + type = lex_optional_float_suffix(parser, seen_e); break; } } @@ -6440,7 +6442,7 @@ lex_numeric_prefix(pm_parser_t *parser) { parser->current.end += pm_strspn_decimal_number_validate(parser, parser->current.end); // Afterward, we'll lex as far as we can into an optional float suffix. - type = lex_optional_float_suffix(parser); + type = lex_optional_float_suffix(parser, seen_e); } return type; @@ -6452,7 +6454,8 @@ lex_numeric(pm_parser_t *parser) { parser->integer_base = PM_INTEGER_BASE_FLAGS_DECIMAL; if (parser->current.end < parser->end) { - type = lex_numeric_prefix(parser); + bool seen_e = false; + type = lex_numeric_prefix(parser, &seen_e); const uint8_t *end = parser->current.end; pm_token_type_t suffix_type = type; @@ -6468,7 +6471,7 @@ lex_numeric(pm_parser_t *parser) { suffix_type = PM_TOKEN_INTEGER_IMAGINARY; } } else { - if (match(parser, 'r')) { + if (!seen_e && match(parser, 'r')) { suffix_type = PM_TOKEN_FLOAT_RATIONAL; if (match(parser, 'i')) { -- cgit v1.2.3