[前][次][番号順一覧][スレッド一覧]

ruby-changes:70578

From: Nobuyoshi <ko1@a...>
Date: Sat, 25 Dec 2021 18:04:15 +0900 (JST)
Subject: [ruby-changes:70578] 0867b638af (master): Raise proper exception when month argument is not a name

https://git.ruby-lang.org/ruby.git/commit/?id=0867b638af

From 0867b638aff9ec192ca420a44ffa5a77c892e8f2 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sat, 25 Dec 2021 16:19:33 +0900
Subject: Raise proper exception when month argument is not a name

https://bugs.ruby-lang.org/issues/17485#change-89871
---
 test/ruby/test_time.rb |  5 ++++-
 time.c                 | 12 +++---------
 2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb
index b3dc5d99e3a..2fdf0c2010f 100644
--- a/test/ruby/test_time.rb
+++ b/test/ruby/test_time.rb
@@ -49,7 +49,10 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L49
     t = Time.new(*tm, "-12:00")
     assert_equal([2001,2,28,23,59,30,-43200], [t.year, t.month, t.mday, t.hour, t.min, t.sec, t.gmt_offset], bug4090)
     assert_raise(ArgumentError) { Time.new(2000,1,1, 0,0,0, "+01:60") }
-    assert_raise(ArgumentError) { Time.new(2021, 1, 1, "+09:99") }
+    msg = /invalid value for Integer/
+    assert_raise_with_message(ArgumentError, msg) { Time.new(2021, 1, 1, "+09:99") }
+    assert_raise_with_message(ArgumentError, msg) { Time.new(2021, 1, "+09:99") }
+    assert_raise_with_message(ArgumentError, msg) { Time.new(2021, "+09:99") }
   end
 
   def test_time_add()
diff --git a/time.c b/time.c
index 8f044e1e59c..77f42fc89ab 100644
--- a/time.c
+++ b/time.c
@@ -2789,9 +2789,10 @@ month_arg(VALUE arg) https://github.com/ruby/ruby/blob/trunk/time.c#L2789
         return obj2ubits(arg, 4);
     }
 
+    mon = 0;
     VALUE s = rb_check_string_type(arg);
     if (!NIL_P(s) && RSTRING_LEN(s) > 0) {
-        mon = 0;
+        arg = s;
         for (i=0; i<12; i++) {
             if (RSTRING_LEN(s) == 3 &&
                 STRNCASECMP(months[i], RSTRING_PTR(s), 3) == 0) {
@@ -2799,15 +2800,8 @@ month_arg(VALUE arg) https://github.com/ruby/ruby/blob/trunk/time.c#L2800
                 break;
             }
         }
-        if (mon == 0) {
-            char c = RSTRING_PTR(s)[0];
-
-            if ('0' <= c && c <= '9') {
-                mon = obj2ubits(s, 4);
-            }
-        }
     }
-    else {
+    if (mon == 0) {
         mon = obj2ubits(arg, 4);
     }
     return mon;
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]