diff options
| author | Paul Eggert | 2019-08-21 00:06:00 -0700 |
|---|---|---|
| committer | Paul Eggert | 2019-08-21 00:11:45 -0700 |
| commit | 39fee209942ab7c35b4789f0010264cd6a52197b (patch) | |
| tree | 96f1858c890436713ba0da0fca93d1f33d7dd33a /src/bignum.c | |
| parent | 3881542edeac3e94291c2ce574edf0b0e52764a8 (diff) | |
| download | emacs-39fee209942ab7c35b4789f0010264cd6a52197b.tar.gz emacs-39fee209942ab7c35b4789f0010264cd6a52197b.zip | |
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.c')
| -rw-r--r-- | src/bignum.c | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/bignum.c b/src/bignum.c index 90b1ebea876..167b73eee02 100644 --- a/src/bignum.c +++ b/src/bignum.c | |||
| @@ -63,7 +63,7 @@ init_bignum (void) | |||
| 63 | double | 63 | double |
| 64 | bignum_to_double (Lisp_Object n) | 64 | bignum_to_double (Lisp_Object n) |
| 65 | { | 65 | { |
| 66 | return mpz_get_d_rounded (XBIGNUM (n)->value); | 66 | return mpz_get_d_rounded (*xbignum_val (n)); |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | /* Return D, converted to a Lisp integer. Discard any fraction. | 69 | /* Return D, converted to a Lisp integer. Discard any fraction. |
| @@ -264,13 +264,13 @@ intmax_t | |||
| 264 | bignum_to_intmax (Lisp_Object x) | 264 | bignum_to_intmax (Lisp_Object x) |
| 265 | { | 265 | { |
| 266 | intmax_t i; | 266 | intmax_t i; |
| 267 | return mpz_to_intmax (XBIGNUM (x)->value, &i) ? i : 0; | 267 | return mpz_to_intmax (*xbignum_val (x), &i) ? i : 0; |
| 268 | } | 268 | } |
| 269 | uintmax_t | 269 | uintmax_t |
| 270 | bignum_to_uintmax (Lisp_Object x) | 270 | bignum_to_uintmax (Lisp_Object x) |
| 271 | { | 271 | { |
| 272 | uintmax_t i; | 272 | uintmax_t i; |
| 273 | return mpz_to_uintmax (XBIGNUM (x)->value, &i) ? i : 0; | 273 | return mpz_to_uintmax (*xbignum_val (x), &i) ? i : 0; |
| 274 | } | 274 | } |
| 275 | 275 | ||
| 276 | /* Yield an upper bound on the buffer size needed to contain a C | 276 | /* Yield an upper bound on the buffer size needed to contain a C |
| @@ -284,7 +284,7 @@ mpz_bufsize (mpz_t const num, int base) | |||
| 284 | ptrdiff_t | 284 | ptrdiff_t |
| 285 | bignum_bufsize (Lisp_Object num, int base) | 285 | bignum_bufsize (Lisp_Object num, int base) |
| 286 | { | 286 | { |
| 287 | return mpz_bufsize (XBIGNUM (num)->value, base); | 287 | return mpz_bufsize (*xbignum_val (num), base); |
| 288 | } | 288 | } |
| 289 | 289 | ||
| 290 | /* Convert NUM to a nearest double, as opposed to mpz_get_d which | 290 | /* Convert NUM to a nearest double, as opposed to mpz_get_d which |
| @@ -318,7 +318,7 @@ ptrdiff_t | |||
| 318 | bignum_to_c_string (char *buf, ptrdiff_t size, Lisp_Object num, int base) | 318 | bignum_to_c_string (char *buf, ptrdiff_t size, Lisp_Object num, int base) |
| 319 | { | 319 | { |
| 320 | eassert (bignum_bufsize (num, abs (base)) == size); | 320 | eassert (bignum_bufsize (num, abs (base)) == size); |
| 321 | mpz_get_str (buf, base, XBIGNUM (num)->value); | 321 | mpz_get_str (buf, base, *xbignum_val (num)); |
| 322 | ptrdiff_t n = size - 2; | 322 | ptrdiff_t n = size - 2; |
| 323 | return !buf[n - 1] ? n - 1 : n + !!buf[n]; | 323 | return !buf[n - 1] ? n - 1 : n + !!buf[n]; |
| 324 | } | 324 | } |