ruby-changes:33555
From: akr <ko1@a...>
Date: Sat, 19 Apr 2014 10:11:13 +0900 (JST)
Subject: [ruby-changes:33555] akr:r45636 (trunk): * internal.h (struct RBignum): Use size_t for len.
akr 2014-04-19 10:11:04 +0900 (Sat, 19 Apr 2014) New Revision: 45636 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45636 Log: * 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. Modified files: trunk/ChangeLog trunk/bignum.c trunk/include/ruby/intern.h trunk/internal.h trunk/marshal.c trunk/rational.c Index: include/ruby/intern.h =================================================================== --- include/ruby/intern.h (revision 45635) +++ include/ruby/intern.h (revision 45636) @@ -91,12 +91,12 @@ VALUE rb_ary_resize(VALUE ary, long len) https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L91 #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); Index: ChangeLog =================================================================== --- ChangeLog (revision 45635) +++ ChangeLog (revision 45636) @@ -1,3 +1,17 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Apr 19 10:07:24 2014 Tanaka Akira <akr@f...> + + * 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 <akr@f...> * numeric.c (rb_num2long): Returns a long. Index: internal.h =================================================================== --- internal.h (revision 45635) +++ internal.h (revision 45636) @@ -372,7 +372,7 @@ struct RBignum { https://github.com/ruby/ruby/blob/trunk/internal.h#L372 struct RBasic basic; union { struct { - long len; + size_t len; BDIGIT *digits; } heap; BDIGIT ary[BIGNUM_EMBED_LEN_MAX]; Index: bignum.c =================================================================== --- bignum.c (revision 45635) +++ bignum.c (revision 45636) @@ -148,7 +148,7 @@ static void bary_divmod(BDIGIT *qds, siz https://github.com/ruby/ruby/blob/trunk/bignum.c#L148 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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L2879 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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L2935 (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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L2970 } 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 https://github.com/ruby/ruby/blob/trunk/bignum.c#L2995 } 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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L3003 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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L3068 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 https://github.com/ruby/ruby/blob/trunk/bignum.c#L4940 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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5002 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 https://github.com/ruby/ruby/blob/trunk/bignum.c#L5724 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 https://github.com/ruby/ruby/blob/trunk/bignum.c#L6738 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; Index: marshal.c =================================================================== --- marshal.c (revision 45635) +++ marshal.c (revision 45636) @@ -35,8 +35,8 @@ https://github.com/ruby/ruby/blob/trunk/marshal.c#L35 #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 https://github.com/ruby/ruby/blob/trunk/marshal.c#L774 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; Index: rational.c =================================================================== --- rational.c (revision 45635) +++ rational.c (revision 45636) @@ -363,8 +363,8 @@ f_gcd(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/rational.c#L363 { #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); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/