summaryrefslogtreecommitdiff
path: root/src/bignum.h
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2019-08-21 00:06:00 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2019-08-21 00:11:45 -0700
commit39fee209942ab7c35b4789f0010264cd6a52197b (patch)
tree96f1858c890436713ba0da0fca93d1f33d7dd33a /src/bignum.h
parent3881542edeac3e94291c2ce574edf0b0e52764a8 (diff)
downloademacs-39fee209942ab7c35b4789f0010264cd6a52197b.tar.gz
Be more careful about pointers to bignum vals
This uses ‘const’ to be better at catching bugs that mistakenly attempt to modify a bignum value. Lisp bignums are supposed to be immutable. * src/alloc.c (make_pure_bignum): * src/fns.c (sxhash_bignum): Accept Lisp_Object instead of struct Lisp_Bignum *, as that’s simpler now. Caller changed. * src/bignum.h (bignum_val, xbignum_val): New inline functions. Prefer them to &i->value and XBIGNUM (i)->value, since they apply ‘const’ to the result. * src/timefns.c (lisp_to_timespec): Use mpz_t const * to point to a bignum value.
Diffstat (limited to 'src/bignum.h')
-rw-r--r--src/bignum.h17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/bignum.h b/src/bignum.h
index 9a32ffb0374..bf7b3669537 100644
--- a/src/bignum.h
+++ b/src/bignum.h
@@ -80,6 +80,19 @@ mpz_set_uintmax (mpz_t result, uintmax_t v)
mpz_set_uintmax_slow (result, v);
}
+/* Return a pointer to the mpz_t value represented by the bignum I.
+ It is const because the value should not change. */
+INLINE mpz_t const *
+bignum_val (struct Lisp_Bignum const *i)
+{
+ return &i->value;
+}
+INLINE mpz_t const *
+xbignum_val (Lisp_Object i)
+{
+ return bignum_val (XBIGNUM (i));
+}
+
/* Return a pointer to an mpz_t that is equal to the Lisp integer I.
If I is a bignum this returns a pointer to I's representation;
otherwise this sets *TMP to I's value and returns TMP. */
@@ -91,7 +104,7 @@ bignum_integer (mpz_t *tmp, Lisp_Object i)
mpz_set_intmax (*tmp, XFIXNUM (i));
return tmp;
}
- return &XBIGNUM (i)->value;
+ return xbignum_val (i);
}
/* Set RESULT to the value stored in the Lisp integer I. If I is a
@@ -103,7 +116,7 @@ mpz_set_integer (mpz_t result, Lisp_Object i)
if (FIXNUMP (i))
mpz_set_intmax (result, XFIXNUM (i));
else
- mpz_set (result, XBIGNUM (i)->value);
+ mpz_set (result, *xbignum_val (i));
}
INLINE_HEADER_END