aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-10-13 03:56:31 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-10-13 03:56:31 +0000
commita01e62831818ed1c8c00e79991b8f34c02327127 (patch)
tree2ad7fb3cbeec9fa7cfc592bba2c3d0830a50aaeb
parentacd98555b59522b8f81b8feff70964e92adb902f (diff)
downloadruby-a01e62831818ed1c8c00e79991b8f34c02327127.tar.gz
* numeric.c (rb_num_to_uint): added to check the range of arguments.
Mainly for negative value with NUM2UINT on 32bit environment. * string.c (rb_str_concat): use rb_num_to_uint. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29480 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--numeric.c30
-rw-r--r--string.c15
3 files changed, 47 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 09bfdb9a8f..7e8646db1b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Wed Oct 13 12:53:43 2010 NARUSE, Yui <naruse@ruby-lang.org>
+
+ * numeric.c (rb_num_to_uint): added to check the range of arguments.
+ Mainly for negative value with NUM2UINT on 32bit environment.
+
+ * string.c (rb_str_concat): use rb_num_to_uint.
+
Wed Oct 13 12:10:02 2010 NAKAMURA Usaku <usa@ruby-lang.org>
* thread_win32.c (w32_error): get English message first, instead
diff --git a/numeric.c b/numeric.c
index 5125507f04..4b3cf595cb 100644
--- a/numeric.c
+++ b/numeric.c
@@ -113,6 +113,36 @@ rb_num_zerodiv(void)
rb_raise(rb_eZeroDivError, "divided by 0");
}
+/* experimental API */
+int
+rb_num_to_uint(VALUE val, unsigned int *ret)
+{
+#define NUMERR_TYPE 1
+#define NUMERR_NEGATIVE 2
+#define NUMERR_TOOLARGE 3
+ if (FIXNUM_P(val)) {
+ long v = FIX2LONG(val);
+ if (v > UINT_MAX) return NUMERR_TOOLARGE;
+ if (v < 0) return NUMERR_NEGATIVE;
+ *ret = (unsigned int)v;
+ return 0;
+ }
+
+ switch (TYPE(val)) {
+ case T_BIGNUM:
+ if (RBIGNUM_NEGATIVE_P(val)) return NUMERR_NEGATIVE;
+#if SIZEOF_INT < SIZEOF_LONG
+ /* long is 64bit */
+ return NUMERR_TOOLARGE;
+#else
+ /* long is 32bit */
+ if (RBIGNUM_LEN(x) > DIGSPERLONG) return NUMERR_TOOLARGE;
+ *ret = (unsigned int)rb_big2ulong((VALUE)val);
+ return 0;
+#endif
+ }
+ return NUMERR_TYPE;
+}
/*
* call-seq:
diff --git a/string.c b/string.c
index 6fddfa8169..efb0481937 100644
--- a/string.c
+++ b/string.c
@@ -2001,6 +2001,7 @@ rb_str_append(VALUE str, VALUE str2)
return rb_str_buf_append(str, str2);
}
+int rb_num_to_uint(VALUE val, unsigned int *ret);
/*
* call-seq:
@@ -2023,11 +2024,15 @@ rb_str_concat(VALUE str1, VALUE str2)
{
unsigned int lc;
- if (FIXNUM_P(str2)) {
- lc = FIX2UINT(str2);
- }
- else if (TYPE(str2) == T_BIGNUM) {
- lc = NUM2UINT(str2);
+ if (FIXNUM_P(str2) || TYPE(str2) == T_BIGNUM) {
+ if (rb_num_to_uint(str2, &lc) == 0) {
+ }
+ else if (FIXNUM_P(str2)) {
+ rb_raise(rb_eRangeError, "%ld out of char range", FIX2LONG(str2));
+ }
+ else {
+ rb_raise(rb_eRangeError, "bignum out of char range");
+ }
}
else {
return rb_str_append(str1, str2);