From 95de2b001247b5e8ccd89d5ba7225d42aafec1e5 Mon Sep 17 00:00:00 2001 From: akr Date: Sat, 19 Apr 2014 01:11:04 +0000 Subject: * internal.h (struct RBignum): Use size_t for len. * include/ruby/intern.h (rb_big_new): Use size_t instead of long to specify the size of bignum. (rb_big_resize): Ditto. * bignum.c: Follow above changes. * rational.c: Follow above changes. * marshal.c: Follow above changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45636 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 14 ++++++++++++++ bignum.c | 39 ++++++++++++++++++++++----------------- include/ruby/intern.h | 4 ++-- internal.h | 2 +- marshal.c | 14 ++++++++++---- rational.c | 4 ++-- 6 files changed, 51 insertions(+), 26 deletions(-) diff --git a/ChangeLog b/ChangeLog index 497953f97e..aa2e76a7f4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Sat Apr 19 10:07:24 2014 Tanaka Akira + + * internal.h (struct RBignum): Use size_t for len. + + * include/ruby/intern.h (rb_big_new): Use size_t instead of long to + specify the size of bignum. + (rb_big_resize): Ditto. + + * bignum.c: Follow above changes. + + * rational.c: Follow above changes. + + * marshal.c: Follow above changes. + Sat Apr 19 00:32:07 2014 Tanaka Akira * numeric.c (rb_num2long): Returns a long. diff --git a/bignum.c b/bignum.c index 57f6488f62..1357de0e87 100644 --- a/bignum.c +++ b/bignum.c @@ -148,7 +148,7 @@ static void bary_divmod(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BD static VALUE bigmul0(VALUE x, VALUE y); static void bary_mul_toom3(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn); -static VALUE bignew_1(VALUE klass, long len, int sign); +static VALUE bignew_1(VALUE klass, size_t len, int sign); static inline VALUE bigtrunc(VALUE x); static VALUE bigsq(VALUE x); @@ -2879,7 +2879,7 @@ dump_bignum(VALUE x) for (i = BIGNUM_LEN(x); i--; ) { printf("_%0*"PRIxBDIGIT, SIZEOF_BDIGIT*2, BDIGITS(x)[i]); } - printf(", len=%lu", BIGNUM_LEN(x)); + printf(", len=%"PRIuSIZE, BIGNUM_LEN(x)); puts(""); } @@ -2935,7 +2935,7 @@ rb_cmpint(VALUE val, VALUE a, VALUE b) (void)(RBIGNUM(b)->as.heap.len = (l))) static void -rb_big_realloc(VALUE big, long len) +rb_big_realloc(VALUE big, size_t len) { BDIGIT *ds; if (RBASIC(big)->flags & BIGNUM_EMBED_FLAG) { @@ -2970,14 +2970,14 @@ rb_big_realloc(VALUE big, long len) } void -rb_big_resize(VALUE big, long len) +rb_big_resize(VALUE big, size_t len) { rb_big_realloc(big, len); BIGNUM_SET_LEN(big, len); } static VALUE -bignew_1(VALUE klass, long len, int sign) +bignew_1(VALUE klass, size_t len, int sign) { NEWOBJ_OF(big, struct RBignum, klass, T_BIGNUM | (RGENGC_WB_PROTECTED_BIGNUM ? FL_WB_PROTECTED : 0)); BIGNUM_SET_SIGN(big, sign?1:0); @@ -2995,7 +2995,7 @@ bignew_1(VALUE klass, long len, int sign) } VALUE -rb_big_new(long len, int sign) +rb_big_new(size_t len, int sign) { return bignew(len, sign != 0); } @@ -3003,7 +3003,7 @@ rb_big_new(long len, int sign) VALUE rb_big_clone(VALUE x) { - long len = BIGNUM_LEN(x); + size_t len = BIGNUM_LEN(x); VALUE z = bignew_1(CLASS_OF(x), len, BIGNUM_SIGN(x)); MEMCPY(BDIGITS(z), BDIGITS(x), BDIGIT, len); @@ -3068,7 +3068,7 @@ twocomp2abs_bang(VALUE x, int hibits) static inline VALUE bigtrunc(VALUE x) { - long len = BIGNUM_LEN(x); + size_t len = BIGNUM_LEN(x); BDIGIT *ds = BDIGITS(x); if (len == 0) return x; @@ -4940,7 +4940,7 @@ rb_big_to_s(int argc, VALUE *argv, VALUE x) static unsigned long big2ulong(VALUE x, const char *type) { - long len = BIGNUM_LEN(x); + size_t len = BIGNUM_LEN(x); unsigned long num; BDIGIT *ds; @@ -5002,7 +5002,7 @@ rb_big2long(VALUE x) static unsigned LONG_LONG big2ull(VALUE x, const char *type) { - long len = BIGNUM_LEN(x); + size_t len = BIGNUM_LEN(x); unsigned LONG_LONG num; BDIGIT *ds = BDIGITS(x); @@ -5724,7 +5724,7 @@ static VALUE bigadd(VALUE x, VALUE y, int sign) { VALUE z; - long len; + size_t len; sign = (sign == BIGNUM_SIGN(y)); if (BIGNUM_SIGN(x) != sign) { @@ -6738,24 +6738,29 @@ static VALUE rb_big_aref(VALUE x, VALUE y) { BDIGIT *xds; - unsigned long shift; - long i, s1, s2; + size_t shift; + size_t i, s1, s2; + long l; BDIGIT bit; if (RB_BIGNUM_TYPE_P(y)) { if (!BIGNUM_SIGN(y)) return INT2FIX(0); bigtrunc(y); - if (BIGSIZE(y) > sizeof(long)) { + if (BIGSIZE(y) > sizeof(size_t)) { out_of_range: return BIGNUM_SIGN(x) ? INT2FIX(0) : INT2FIX(1); } +#if SIZEOF_SIZE_T <= SIZEOF_LONG shift = big2ulong(y, "long"); +#else + shift = big2ull(y, "long long"); +#endif } else { - i = NUM2LONG(y); - if (i < 0) return INT2FIX(0); - shift = i; + l = NUM2LONG(y); + if (l < 0) return INT2FIX(0); + shift = (size_t)l; } s1 = shift/BITSPERDIG; s2 = shift%BITSPERDIG; diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 374dc4c402..f36f19214d 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -91,12 +91,12 @@ VALUE rb_ary_resize(VALUE ary, long len); #define rb_ary_new3 rb_ary_new_from_args #define rb_ary_new4 rb_ary_new_from_values /* bignum.c */ -VALUE rb_big_new(long, int); +VALUE rb_big_new(size_t, int); int rb_bigzero_p(VALUE x); VALUE rb_big_clone(VALUE); void rb_big_2comp(VALUE); VALUE rb_big_norm(VALUE); -void rb_big_resize(VALUE big, long len); +void rb_big_resize(VALUE big, size_t len); VALUE rb_cstr_to_inum(const char*, int, int); VALUE rb_str_to_inum(VALUE, int, int); VALUE rb_cstr2inum(const char*, int); diff --git a/internal.h b/internal.h index 36424437f9..f93d3f7751 100644 --- a/internal.h +++ b/internal.h @@ -372,7 +372,7 @@ struct RBignum { struct RBasic basic; union { struct { - long len; + size_t len; BDIGIT *digits; } heap; BDIGIT ary[BIGNUM_EMBED_LEN_MAX]; diff --git a/marshal.c b/marshal.c index d828043492..c45cd03067 100644 --- a/marshal.c +++ b/marshal.c @@ -35,8 +35,8 @@ #if SIZEOF_SHORT == SIZEOF_BDIGIT #define SHORTLEN(x) (x) #else -static long -shortlen(long len, BDIGIT *ds) +static size_t +shortlen(size_t len, BDIGIT *ds) { BDIGIT num; int offset = 0; @@ -774,11 +774,17 @@ w_object(VALUE obj, struct dump_arg *arg, int limit) w_byte(TYPE_BIGNUM, arg); { char sign = BIGNUM_SIGN(obj) ? '+' : '-'; - long len = BIGNUM_LEN(obj); + size_t len = BIGNUM_LEN(obj); + size_t slen; BDIGIT *d = BIGNUM_DIGITS(obj); + slen = SHORTLEN(len); + if (LONG_MAX < slen) { + rb_raise(rb_eTypeError, "too big Bignum can't be dumped"); + } + w_byte(sign, arg); - w_long(SHORTLEN(len), arg); /* w_short? */ + w_long((long)slen, arg); while (len--) { #if SIZEOF_BDIGIT > SIZEOF_SHORT BDIGIT num = *d; diff --git a/rational.c b/rational.c index 932fc53fbb..60ff356b22 100644 --- a/rational.c +++ b/rational.c @@ -363,8 +363,8 @@ f_gcd(VALUE x, VALUE y) { #ifdef USE_GMP if (RB_TYPE_P(x, T_BIGNUM) && RB_TYPE_P(y, T_BIGNUM)) { - long xn = BIGNUM_LEN(x); - long yn = BIGNUM_LEN(y); + size_t xn = BIGNUM_LEN(x); + size_t yn = BIGNUM_LEN(y); if (GMP_GCD_DIGITS <= xn || GMP_GCD_DIGITS <= yn) return rb_gcd_gmp(x, y); } -- cgit v1.2.3