summaryrefslogtreecommitdiff
path: root/time.c
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2020-01-02 11:48:03 -0800
committerAaron Patterson <tenderlove@github.com>2020-01-13 13:58:23 -0800
commit91601dcc6a608cb6f9a124959c738755091dfbd9 (patch)
treeef55399c8a55d7c223b5376f2b341a142474e26b /time.c
parent5f3189474c3ee3e11b6588acfbb026e119522092 (diff)
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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2808
Diffstat (limited to 'time.c')
-rw-r--r--time.c16
1 files changed, 6 insertions, 10 deletions
diff --git a/time.c b/time.c
index 817b6ef101..b5d1aee375 100644
--- a/time.c
+++ b/time.c
@@ -656,7 +656,7 @@ VALUE rb_cTime;
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)
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