summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authorshyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-04 04:31:06 +0000
committershyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-04 04:31:06 +0000
commit519f06ed80093df52922eb0e48d5264037f4916d (patch)
tree58381f20f713af92194cbadefadec945315dc122 /numeric.c
parent3056186947891523de7a4f86b8bebea8f422559a (diff)
merge revision(s) 18100,18129:
* numeric.c (check_uint, rb_num2uint, rb_fix2uint): strict check. fixed [ruby-dev:33683] * numeric.c (check_uint, rb_num2uint, rb_fix2uint): fixed wrong check about 64bit positive value. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_6@18329 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/numeric.c b/numeric.c
index 98d5f96..21a4f2a 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1597,11 +1597,21 @@ check_int(num)
}
static void
-check_uint(num)
+check_uint(num, sign)
unsigned long num;
+ VALUE sign;
{
- if (num > UINT_MAX) {
- rb_raise(rb_eRangeError, "integer %lu too big to convert to `unsigned int'", num);
+ static const unsigned long mask = ~(unsigned long)UINT_MAX;
+
+ if (RTEST(sign)) {
+ /* minus */
+ if ((num & mask) != mask || (num & ~mask) <= INT_MAX + 1UL)
+ rb_raise(rb_eRangeError, "integer %ld too small to convert to `unsigned int'", num);
+ }
+ else {
+ /* plus */
+ if ((num & mask) != 0)
+ rb_raise(rb_eRangeError, "integer %lu too big to convert to `unsigned int'", num);
}
}
@@ -1631,9 +1641,7 @@ rb_num2uint(val)
{
unsigned long num = rb_num2ulong(val);
- if (RTEST(rb_funcall(INT2FIX(0), '<', 1, val))) {
- check_uint(num);
- }
+ check_uint(num, rb_funcall(val, '<', 1, INT2FIX(0)));
return num;
}
@@ -1647,9 +1655,8 @@ rb_fix2uint(val)
return rb_num2uint(val);
}
num = FIX2ULONG(val);
- if (FIX2LONG(val) > 0) {
- check_uint(num);
- }
+
+ check_uint(num, rb_funcall(val, '<', 1, INT2FIX(0)));
return num;
}
#else