summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-17 01:23:35 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-17 01:23:35 +0000
commit6852eaaf0d69bc8f1b394cd63f2742e8da5cb67b (patch)
treef719d28a264687405f56bd9a590d90dcc86ddfed /numeric.c
parentc60b57ade0f4b31c2177615dca9839d40baaa49d (diff)
* numeric.c (check_uint, rb_num2uint, rb_fix2uint): strict check.
fixed [ruby-dev:33683] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18099 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c29
1 files changed, 19 insertions, 10 deletions
diff --git a/numeric.c b/numeric.c
index 83f1d3c..3e1c467 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1587,8 +1587,22 @@ check_int(SIGNED_VALUE num)
static void
check_uint(VALUE num)
{
- if (num > UINT_MAX) {
- rb_raise(rb_eRangeError, "integer %"PRIuVALUE " too big to convert to `unsigned int'", num);
+ static const VALUE mask = ~(VALUE)UINT_MAX;
+ static const VALUE msb =
+#if SIZEOF_LONG == SIZEOF_VALUE
+ ~LONG_MAX;
+#elif SIZEOF_LONG_LONG == SIZEOF_VALUE
+ ~LLONG_MAX;
+#endif
+ const char *s;
+
+ if ((num & mask) != 0 &&
+ ((num & mask) != mask || (num & ~mask) <= INT_MAX + 1UL)) {
+ if ((num & msb) == 0)
+ s = "big";
+ else
+ s = "small";
+ rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `unsigned int'", num, s);
}
}
@@ -1615,10 +1629,7 @@ rb_num2uint(VALUE val)
{
unsigned long num = rb_num2ulong(val);
- if (RTEST(rb_funcall(val, '<', 1, INT2FIX(0))))
- check_int(num);
- else
- check_uint(num);
+ check_uint(num);
return num;
}
@@ -1631,10 +1642,8 @@ rb_fix2uint(VALUE val)
return rb_num2uint(val);
}
num = FIX2ULONG(val);
- if (RTEST(rb_funcall(val, '<', 1, INT2FIX(0))))
- check_int(num);
- else
- check_uint(num);
+
+ check_uint(num);
return num;
}
#else