ruby-changes:30242
From: akr <ko1@a...>
Date: Thu, 1 Aug 2013 00:02:08 +0900 (JST)
Subject: [ruby-changes:30242] akr:r42294 (trunk): * bignum.c (big2str_find_n1): Change the return type to size_t.
akr 2013-08-01 00:01:55 +0900 (Thu, 01 Aug 2013) New Revision: 42294 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42294 Log: * bignum.c (big2str_find_n1): Change the return type to size_t. (big2str_orig): Ditto. (big2str_karatsuba): Ditto. (rb_big2str1): Follow the above changes. Modified files: trunk/ChangeLog trunk/bignum.c Index: ChangeLog =================================================================== --- ChangeLog (revision 42293) +++ ChangeLog (revision 42294) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Jul 31 23:59:28 2013 Tanaka Akira <akr@f...> + + * bignum.c (big2str_find_n1): Change the return type to size_t. + (big2str_orig): Ditto. + (big2str_karatsuba): Ditto. + (rb_big2str1): Follow the above changes. + Wed Jul 31 23:19:06 2013 Tanaka Akira <akr@f...> * bignum.c (power_cache_get_power): Change numdigits_ret to size_t *. Index: bignum.c =================================================================== --- bignum.c (revision 42293) +++ bignum.c (revision 42294) @@ -4167,7 +4167,7 @@ power_cache_get_power(int base, int powe https://github.com/ruby/ruby/blob/trunk/bignum.c#L4167 * (2*log_2(b_1)), therefore n_1 is given by ceil((B*n_0) / * (2*log_2(b_1))). */ -static long +static size_t big2str_find_n1(VALUE x, int base) { static const double log_2[] = { @@ -4212,7 +4212,7 @@ struct big2str_struct { https://github.com/ruby/ruby/blob/trunk/bignum.c#L4212 int hbase_numdigits; }; -static long +static size_t big2str_orig(struct big2str_struct *b2s, VALUE x, char* ptr, size_t len, int trim) { long i = RBIGNUM_LEN(x); @@ -4245,12 +4245,11 @@ big2str_orig(struct big2str_struct *b2s, https://github.com/ruby/ruby/blob/trunk/bignum.c#L4245 return len; } -static long +static size_t big2str_karatsuba(struct big2str_struct *b2s, VALUE x, char* ptr, int power_level, size_t len, int trim) { - long lh, ll; - size_t m1; + size_t lh, ll, m1; VALUE b, q, r; if (BIGZEROP(x)) { @@ -4315,7 +4314,7 @@ rb_big2str1(VALUE x, int base, int trim) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4314 { int off; VALUE ss, xx; - long n2, len; + size_t n2, len; char* ptr; struct big2str_struct b2s_data; int power_level; @@ -4335,10 +4334,13 @@ rb_big2str1(VALUE x, int base, int trim) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4334 if (POW2_P(base)) { /* base == 2 || base == 4 || base == 8 || base == 16 || base == 32 */ - return big2str_base_powerof2(x, (size_t)n2, base, trim); + return big2str_base_powerof2(x, n2, base, trim); } - ss = rb_usascii_str_new(0, n2 + 1); /* plus one for sign */ + if (LONG_MAX-1 < n2) + rb_raise(rb_eArgError, "too big number"); + + ss = rb_usascii_str_new(0, (long)(n2 + 1)); /* plus one for sign */ ptr = RSTRING_PTR(ss); ptr[0] = RBIGNUM_SIGN(x) ? '+' : '-'; @@ -4366,16 +4368,18 @@ rb_big2str1(VALUE x, int base, int trim) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4368 xx = rb_big_clone(x); RBIGNUM_SET_SIGN(xx, 1); if (power_level < 0) { - len = off + big2str_orig(&b2s_data, xx, ptr + off, (size_t)n2, trim); + len = off + big2str_orig(&b2s_data, xx, ptr + off, n2, trim); } else { len = off + big2str_karatsuba(&b2s_data, xx, ptr + off, power_level, - (size_t)n2, trim); + n2, trim); } rb_big_resize(xx, 0); + assert(len <= LONG_MAX); + ptr[len] = '\0'; - rb_str_resize(ss, len); + rb_str_resize(ss, (long)len); return ss; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/