Skip to content

Commit c81f7bf

Browse files
committed
Split rawSetRandomInteger out from rawRandomInteger
We already do this for random QQ's. This will prevent us from leaking a ZZ and also allow us to throw errors.
1 parent 311b554 commit c81f7bf

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

M2/Macaulay2/e/ZZ.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ std::pair<bool, long> RingZZ::coerceToLongInteger(ring_elem a) const
6767
mpz_get_si(a.get_mpz()));
6868
}
6969

70-
ring_elem RingZZ::random() const { return ring_elem(rawRandomInteger(nullptr)); }
70+
ring_elem RingZZ::random() const {
71+
mpz_ptr result = new_elem();
72+
rawSetRandomInteger(result, nullptr);
73+
mpz_reallocate_limbs(result);
74+
return ring_elem(result);
75+
}
7176

7277
void RingZZ::elem_text_out(buffer &o,
7378
const ring_elem ap,

M2/Macaulay2/e/aring-zz-gmp.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ class ARingZZGMP : public SimpleARing<ARingZZGMP>
195195
void swap(ElementType& a, ElementType& b) const { mpz_swap(&a, &b); }
196196
void random(ElementType& result) const
197197
{
198-
// TODO: this leaks a gmp_ZZ
199-
mpz_set(&result, rawRandomInteger(nullptr));
198+
rawSetRandomInteger(&result, nullptr);
200199
}
201200
/** @} */
202201

M2/Macaulay2/e/interface/random.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ int32_t rawRandomInt(int32_t max)
5353
return RandomSeed % max;
5454
}
5555

56-
gmp_ZZ rawRandomInteger(gmp_ZZ maxN)
56+
void rawSetRandomInteger(mpz_ptr result, gmp_ZZ maxN)
5757
/* if height is the null pointer, use the default height */
5858
{
59-
mpz_ptr result = getmemstructtype(mpz_ptr);
60-
mpz_init(result);
6159
if (maxN == nullptr)
6260
mpz_urandomm(result, state, maxHeight);
6361
else if (1 != mpz_sgn(maxN))
@@ -66,6 +64,16 @@ gmp_ZZ rawRandomInteger(gmp_ZZ maxN)
6664
}
6765
else
6866
mpz_urandomm(result, state, maxN);
67+
}
68+
69+
gmp_ZZ rawRandomInteger(gmp_ZZ maxN)
70+
/* if height is the null pointer, use the default height */
71+
{
72+
mpz_ptr result = getmemstructtype(mpz_ptr);
73+
mpz_init(result);
74+
75+
rawSetRandomInteger(result, maxN);
76+
6977
mpz_reallocate_limbs(result);
7078
return result;
7179
}

M2/Macaulay2/e/interface/random.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ unsigned long rawRandomULong(unsigned long max);
2525
int32_t rawRandomInt(int32_t max);
2626
/* generate a random number in the range 0..max-1 */
2727

28+
void rawSetRandomInteger(mpz_ptr result, gmp_ZZ maxN);
29+
/* if height is the null pointer, use the default height */
30+
2831
gmp_ZZ rawRandomInteger(gmp_ZZ maxN);
2932
/* if height is the null pointer, use the default height */
3033

0 commit comments

Comments
 (0)