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/alloc.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/alloc.c')
| -rw-r--r-- | src/alloc.c | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/alloc.c b/src/alloc.c index bb8e97f8737..53af7325f47 100644 --- a/src/alloc.c +++ b/src/alloc.c | |||
| @@ -5290,9 +5290,10 @@ make_pure_float (double num) | |||
| 5290 | space. */ | 5290 | space. */ |
| 5291 | 5291 | ||
| 5292 | static Lisp_Object | 5292 | static Lisp_Object |
| 5293 | make_pure_bignum (struct Lisp_Bignum *value) | 5293 | make_pure_bignum (Lisp_Object value) |
| 5294 | { | 5294 | { |
| 5295 | size_t i, nlimbs = mpz_size (value->value); | 5295 | mpz_t const *n = xbignum_val (value); |
| 5296 | size_t i, nlimbs = mpz_size (*n); | ||
| 5296 | size_t nbytes = nlimbs * sizeof (mp_limb_t); | 5297 | size_t nbytes = nlimbs * sizeof (mp_limb_t); |
| 5297 | mp_limb_t *pure_limbs; | 5298 | mp_limb_t *pure_limbs; |
| 5298 | mp_size_t new_size; | 5299 | mp_size_t new_size; |
| @@ -5303,10 +5304,10 @@ make_pure_bignum (struct Lisp_Bignum *value) | |||
| 5303 | int limb_alignment = alignof (mp_limb_t); | 5304 | int limb_alignment = alignof (mp_limb_t); |
| 5304 | pure_limbs = pure_alloc (nbytes, - limb_alignment); | 5305 | pure_limbs = pure_alloc (nbytes, - limb_alignment); |
| 5305 | for (i = 0; i < nlimbs; ++i) | 5306 | for (i = 0; i < nlimbs; ++i) |
| 5306 | pure_limbs[i] = mpz_getlimbn (value->value, i); | 5307 | pure_limbs[i] = mpz_getlimbn (*n, i); |
| 5307 | 5308 | ||
| 5308 | new_size = nlimbs; | 5309 | new_size = nlimbs; |
| 5309 | if (mpz_sgn (value->value) < 0) | 5310 | if (mpz_sgn (*n) < 0) |
| 5310 | new_size = -new_size; | 5311 | new_size = -new_size; |
| 5311 | 5312 | ||
| 5312 | mpz_roinit_n (b->value, pure_limbs, new_size); | 5313 | mpz_roinit_n (b->value, pure_limbs, new_size); |
| @@ -5456,7 +5457,7 @@ purecopy (Lisp_Object obj) | |||
| 5456 | return obj; | 5457 | return obj; |
| 5457 | } | 5458 | } |
| 5458 | else if (BIGNUMP (obj)) | 5459 | else if (BIGNUMP (obj)) |
| 5459 | obj = make_pure_bignum (XBIGNUM (obj)); | 5460 | obj = make_pure_bignum (obj); |
| 5460 | else | 5461 | else |
| 5461 | { | 5462 | { |
| 5462 | AUTO_STRING (fmt, "Don't know how to purify: %S"); | 5463 | AUTO_STRING (fmt, "Don't know how to purify: %S"); |