diff --git a/ChangeLog b/ChangeLog index a946be29da..76bedc3a64 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Jul 21 23:47:38 2009 Yusuke Endoh + + * random.c (rand_init): array length of random seed was broken, which + causes memory error with srand(2**1000000-1). + + * test/ruby/test_rand.c: test for above. + Tue Jul 21 21:37:19 2009 Keiju Ishitsuka * lib/irb/cmd/help.rb: fixed irb's "help" command. [ruby-core:22310]. diff --git a/random.c b/random.c index e128c61e5f..ddaaedc5a8 100644 --- a/random.c +++ b/random.c @@ -304,11 +304,9 @@ rand_init(struct MT *mt, VALUE vseed) if (blen == 0) { len = 1; } - else if (blen > MT_MAX_STATE * SIZEOF_INT32 / SIZEOF_BDIGITS) { - blen = (len = MT_MAX_STATE) * SIZEOF_INT32 / SIZEOF_BDIGITS; - len = roomof(len, SIZEOF_INT32); - } else { + if (blen > MT_MAX_STATE * SIZEOF_INT32 / SIZEOF_BDIGITS) + blen = (len = MT_MAX_STATE) * SIZEOF_INT32 / SIZEOF_BDIGITS; len = roomof((int)blen * SIZEOF_BDIGITS, SIZEOF_INT32); } /* allocate ints for init_by_array */ diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index 7bfd96f355..140851d5ba 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -164,4 +164,9 @@ class TestRand < Test::Unit::TestCase srand(0) assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle) end + + def test_big_seed + srand(2**1000000-1) + assert_equal(1143843490, rand(0x100000000)) + end end