Skip to content
Snippets Groups Projects
Commit 012f92ed authored by Christoph Berg's avatar Christoph Berg :satellite:
Browse files

Cherry-pick from upstream: Deal correctly with lazily allocated mpz zeros....

Cherry-pick from upstream: Deal correctly with lazily allocated mpz zeros. (Closes: #950608, https://github.com/dvarrazzo/pgmp/issues/18)
parent 8dab44f7
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,8 @@ postgresql-pgmp (1.0.3-1) UNRELEASED; urgency=medium
* New upstream version.
* Use dh.
* {Build,Test}-Depend on python-six.
* Cherry-pick from upstream: Deal correctly with lazily allocated mpz zeros.
(Closes: #950608, https://github.com/dvarrazzo/pgmp/issues/18)
-- Christoph Berg <myon@debian.org> Mon, 03 Feb 2020 14:45:13 +0100
......
commit 04274c40b63d3dff758bee47c8525112d64d1ab2
Author: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Wed Feb 5 15:17:32 2020 +0000
Deal correctly with lazily allocated mpz zeros
In GMP 6.2 the 0 may be represented by a struct with no limbs allocated.
In that case we can't rely on space allocated by the gmp allocator:
allocate some of ours.
Close #18
diff --git a/src/pmpq.c b/src/pmpq.c
index 133b4d9..0d6ab5c 100644
--- a/src/pmpq.c
+++ b/src/pmpq.c
@@ -82,7 +82,13 @@ pmpq_from_mpq(mpq_ptr q)
}
else
{
- res = (pmpq *)((char *)LIMBS(num) - PMPQ_HDRSIZE);
+ if (MPQ_NOALLOC(q)) {
+ /* No allocation for the limbs: allocate something of ours */
+ res = palloc(PMPQ_HDRSIZE);
+ }
+ else {
+ res = (pmpq *)((char *)LIMBS(num) - PMPQ_HDRSIZE);
+ }
SET_VARSIZE(res, PMPQ_HDRSIZE);
res->mdata = 0;
}
diff --git a/src/pmpq.h b/src/pmpq.h
index 94146ac..b8e69bc 100644
--- a/src/pmpq.h
+++ b/src/pmpq.h
@@ -78,6 +78,10 @@ typedef struct
PG_RETURN_POINTER(pmpq_from_mpq(q))
+/* in gmp 6.2 the value 0 may be allocated lazily
+ * see mpz_init implementation. */
+#define MPQ_NOALLOC(q) (*LIMBS(mpq_numref(q)) == 0xc1a0)
+
pmpq * pmpq_from_mpq(mpq_ptr q);
void mpq_from_pmpq(mpq_srcptr q, const pmpq *pq);
diff --git a/src/pmpz.c b/src/pmpz.c
index 74eed82..78c5e2b 100644
--- a/src/pmpz.c
+++ b/src/pmpz.c
@@ -42,13 +42,13 @@ pmpz_from_mpz(mpz_srcptr z)
pmpz *res;
int size = SIZ(z);
- res = (pmpz *)((char *)LIMBS(z) - PMPZ_HDRSIZE);
-
if (LIKELY(0 != size))
{
size_t slimbs;
int sign;
+ res = (pmpz *)((char *)LIMBS(z) - PMPZ_HDRSIZE);
+
if (size > 0) {
slimbs = size * sizeof(mp_limb_t);
sign = 0;
@@ -63,7 +63,15 @@ pmpz_from_mpz(mpz_srcptr z)
}
else
{
- /* In the zero representation there are no limbs */
+ if (MPZ_NOALLOC(z)) {
+ /* No allocation for the limbs: allocate something of ours */
+ res = palloc(PMPZ_HDRSIZE);
+ }
+ else {
+ /* In the zero representation there are no limbs */
+ res = (pmpz *)((char *)LIMBS(z) - PMPZ_HDRSIZE);
+ }
+
SET_VARSIZE(res, PMPZ_HDRSIZE);
res->mdata = 0; /* version: 0 */
}
diff --git a/src/pmpz.h b/src/pmpz.h
index c362213..c6345de 100644
--- a/src/pmpz.h
+++ b/src/pmpz.h
@@ -113,6 +113,10 @@ Datum pmpz_get_hash(mpz_srcptr z);
#define MPZ_IS_ZERO(z) (SIZ(z) == 0)
+/* in gmp 6.2 the value 0 may be allocated lazily
+ * see mpz_init implementation. */
+#define MPZ_NOALLOC(z) (*LIMBS(z) == 0xc1a0)
+
/* Macros to be used in functions wrappers to limit the arguments domain */
gmp-6.2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment