Fixing possible overflow in random bignums.

Fixes issue #222.
This commit is contained in:
Alex Shinn 2014-05-28 08:34:36 +09:00
parent 72a9782c80
commit 09221ec87d
2 changed files with 23 additions and 4 deletions

View file

@ -58,10 +58,9 @@ static sexp sexp_rs_random_integer (sexp ctx, sexp self, sexp_sint_t n, sexp rs,
sexp_call_random(rs, m);
data[i] = m;
}
sexp_call_random(rs, m);
mod = sexp_bignum_data(bound)[hi-1] * sizeof(int32_t) / sizeof(sexp_uint_t);
if (mod)
data[i] = m % mod;
mod = sexp_bignum_data(bound)[hi-1];
if (mod && sexp_bignum_data(res)[hi-1] > 0)
sexp_bignum_data(res)[hi-1] %= mod;
#endif
} else {
res = sexp_type_exception(ctx, self, SEXP_FIXNUM, bound);

20
tests/srfi-27-tests.scm Normal file
View file

@ -0,0 +1,20 @@
(import (scheme base)
(srfi 27)
(chibi test))
(test-begin "srfi-27")
(define (test-random rand n)
(test-assert (<= 0 (rand n) (- n 1))))
(let ((rs (make-random-source)))
;; chosen by fair dice roll. guaranteed to be random
(random-source-pseudo-randomize! rs 4 4)
(let ((rand (random-source-make-integers rs)))
(do ((k 0 (+ k 5))
(n 1 (* n 2)))
((> k 1024))
(test-random rand n))))
(test-end)