ruby-changes:71271
From: Nobuyoshi <ko1@a...>
Date: Fri, 25 Feb 2022 19:52:44 +0900 (JST)
Subject: [ruby-changes:71271] 1758eade57 (master): [ruby/date] Anchor at beginning of numbers
https://git.ruby-lang.org/ruby.git/commit/?id=1758eade57 From 1758eade579c91f9ad000943994fccf30c7bf8a0 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Fri, 9 Jul 2021 07:21:27 +0900 Subject: [ruby/date] Anchor at beginning of numbers https://hackerone.com/reports/1254844 https://github.com/ruby/date/commit/7ffe25e458 --- ext/date/date_parse.c | 16 +++++++++------- test/date/test_date_parse.rb | 7 +++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/ext/date/date_parse.c b/ext/date/date_parse.c index 021df3793b..9511bedf47 100644 --- a/ext/date/date_parse.c +++ b/ext/date/date_parse.c @@ -253,6 +253,8 @@ s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L253 #define ABBR_DAYS "sun|mon|tue|wed|thu|fri|sat" #define ABBR_MONTHS "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec" +#define NUMBER "(?<!\\d)\\d" + #ifdef TIGHT_PARSER #define VALID_DAYS "(?:" DAYS ")" "|(?:tues|wednes|thurs|thur|" ABBR_DAYS ")\\.?" #define VALID_MONTHS "(?:" MONTHS ")" "|(?:sept|" ABBR_MONTHS ")\\.?" @@ -652,7 +654,7 @@ parse_time(VALUE str, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L654 { static const char pat_source[] = "(" - "\\d+\\s*" + "" NUMBER "+\\s*" "(?:" "(?:" ":\\s*\\d+" @@ -836,7 +838,7 @@ parse_eu(VALUE str, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L838 FPW_COM FPT_COM #endif #ifndef TIGHT_PARSER - "('?\\d+)[^-\\d\\s]*" + "('?" NUMBER "+)[^-\\d\\s]*" #else "(\\d+)(?:(?:st|nd|rd|th)\\b)?" #endif @@ -1332,7 +1334,7 @@ parse_vms11(VALUE str, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L1334 { static const char pat_source[] = #ifndef TIGHT_PARSER - "('?-?\\d+)-(" ABBR_MONTHS ")[^-/.]*" + "('?-?" NUMBER "+)-(" ABBR_MONTHS ")[^-/.]*" "-('?-?\\d+)" #else BOS @@ -1427,7 +1429,7 @@ parse_sla(VALUE str, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L1429 { static const char pat_source[] = #ifndef TIGHT_PARSER - "('?-?\\d+)/\\s*('?\\d+)(?:\\D\\s*('?-?\\d+))?" + "('?-?" NUMBER "+)/\\s*('?\\d+)(?:\\D\\s*('?-?\\d+))?" #else BOS FPW_COM FPT_COM @@ -1535,7 +1537,7 @@ parse_dot(VALUE str, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L1537 { static const char pat_source[] = #ifndef TIGHT_PARSER - "('?-?\\d+)\\.\\s*('?\\d+)\\.\\s*('?-?\\d+)" + "('?-?" NUMBER "+)\\.\\s*('?\\d+)\\.\\s*('?-?\\d+)" #else BOS FPW_COM FPT_COM @@ -1695,7 +1697,7 @@ parse_mday(VALUE str, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L1697 { static const char pat_source[] = #ifndef TIGHT_PARSER - "(\\d+)(st|nd|rd|th)\\b" + "(" NUMBER "+)(st|nd|rd|th)\\b" #else BOS FPW_COM FPT_COM @@ -1933,7 +1935,7 @@ parse_ddd(VALUE str, VALUE hash) https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c#L1935 #ifdef TIGHT_PARSER BOS #endif - "([-+]?)(\\d{2,14})" + "([-+]?)(" NUMBER "{2,14})" "(?:" "\\s*" "t?" diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb index 34a672b069..a87b165080 100644 --- a/test/date/test_date_parse.rb +++ b/test/date/test_date_parse.rb @@ -585,6 +585,13 @@ class TestDateParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/date/test_date_parse.rb#L585 assert_equal(5025, h[:offset]) end + def test__parse_too_long_year + str = "Jan 1" + "0" * 100_000 + h = Timeout.timeout(1) {Date._parse(str)} + assert_equal(100_000, Math.log10(h[:year])) + assert_equal(1, h[:mon]) + end + require 'time' def test_parse__time -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/