ruby-changes:59693
From: John <ko1@a...>
Date: Tue, 14 Jan 2020 06:58:48 +0900 (JST)
Subject: [ruby-changes:59693] 91601dcc6a (master): Simplify obj2ubits checks
https://git.ruby-lang.org/ruby.git/commit/?id=91601dcc6a From 91601dcc6a608cb6f9a124959c738755091dfbd9 Mon Sep 17 00:00:00 2001 From: John Hawthorn <john@h...> Date: Thu, 2 Jan 2020 11:48:03 -0800 Subject: Simplify obj2ubits checks If this value is less than zero, then the mask check is guaranteed to fail as well, so we might as well rely on that. diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index 35e3172..3cf7f2b 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -431,6 +431,10 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L431 assert_equal(-4427700000, Time.utc(-4427700000,12,1).year) assert_equal(-2**30+10, Time.utc(-2**30+10,1,1).year) + + assert_raise(ArgumentError) { Time.gm(2000, 1, -1) } + assert_raise(ArgumentError) { Time.gm(2000, 1, 2**30 + 1) } + assert_raise(ArgumentError) { Time.gm(2000, 1, -2**30 + 1) } end def test_time_interval diff --git a/time.c b/time.c index 817b6ef..b5d1aee 100644 --- a/time.c +++ b/time.c @@ -656,7 +656,7 @@ VALUE rb_cTime; https://github.com/ruby/ruby/blob/trunk/time.c#L656 static VALUE rb_cTimeTM; static int obj2int(VALUE obj); -static uint32_t obj2ubits(VALUE obj, size_t bits); +static uint32_t obj2ubits(VALUE obj, unsigned int bits); static VALUE obj2vint(VALUE obj); static uint32_t month_arg(VALUE arg); static VALUE validate_utc_offset(VALUE utc_offset); @@ -2863,20 +2863,16 @@ obj2int(VALUE obj) https://github.com/ruby/ruby/blob/trunk/time.c#L2863 return NUM2INT(obj); } +/* bits should be 0 <= x <= 31 */ static uint32_t -obj2ubits(VALUE obj, size_t bits) +obj2ubits(VALUE obj, unsigned int bits) { - static const uint32_t u32max = (uint32_t)-1; - const uint32_t usable_mask = ~(u32max << bits); - uint32_t rv; - int tmp = obj2int(obj); + const unsigned int usable_mask = (1U << bits) - 1; + unsigned int rv = (unsigned int)obj2int(obj); - if (tmp < 0) - rb_raise(rb_eArgError, "argument out of range"); - rv = tmp; if ((rv & usable_mask) != rv) rb_raise(rb_eArgError, "argument out of range"); - return rv; + return (uint32_t)rv; } static VALUE -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/