summaryrefslogtreecommitdiffstats
path: root/rand.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2018-05-16 06:47:21 -0700
committerKaz Kylheku <kaz@kylheku.com>2018-05-16 06:47:21 -0700
commitdbff4c5e634e23e3c957260ef8d7ddf59d81dbdb (patch)
tree1f19be4e2951bb1ce2ffd5987053b7242095918f /rand.c
parenteed5dd5b038d947f8288111503c6bf48e7e30f17 (diff)
downloadtxr-dbff4c5e634e23e3c957260ef8d7ddf59d81dbdb.tar.gz
txr-dbff4c5e634e23e3c957260ef8d7ddf59d81dbdb.tar.bz2
txr-dbff4c5e634e23e3c957260ef8d7ddf59d81dbdb.zip
random: reject negative bignum modulus.
* rand.c (random): Function now rejects negative bignums, not only negative fixnums.
Diffstat (limited to 'rand.c')
-rw-r--r--rand.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/rand.c b/rand.c
index e8455b56..a5109701 100644
--- a/rand.c
+++ b/rand.c
@@ -220,9 +220,9 @@ val random(val state, val modulus)
val self = lit("random");
struct rand_state *r = coerce(struct rand_state *,
cobj_handle(state, random_state_s));
+ mp_int *m;
- if (bignump(modulus)) {
- mp_int *m = mp(modulus);
+ if (bignump(modulus) && !ISNEG(m = mp(modulus))) {
ucnum bits = mp_count_bits(m) - mp_is_pow_two(m);
ucnum rands_needed = (bits + 32 - 1) / 32;
ucnum msb_rand_bits = bits % 32;