diff options
| author | Paul Eggert | 2019-08-18 12:11:06 -0700 |
|---|---|---|
| committer | Paul Eggert | 2019-08-18 12:12:45 -0700 |
| commit | 780509f29f0aa006a578744f7e871eb6d5ce5931 (patch) | |
| tree | f8b9af7529ea7d617038eebf12b62299a19750d9 /src | |
| parent | f92d61c06c82d515ee83e340b8af4b1489778404 (diff) | |
| download | emacs-780509f29f0aa006a578744f7e871eb6d5ce5931.tar.gz emacs-780509f29f0aa006a578744f7e871eb6d5ce5931.zip | |
Improve bignum_integer static checking
* src/bignum.h (bignum_integer): Now returns pointer-to-const,
to catch trivial mistakes where the caller might try to modify
a Lisp bignum. Lisp bignums are supposed to be immutable.
All callers changed.
Diffstat (limited to 'src')
| -rw-r--r-- | src/bignum.h | 2 | ||||
| -rw-r--r-- | src/data.c | 8 | ||||
| -rw-r--r-- | src/timefns.c | 18 |
3 files changed, 14 insertions, 14 deletions
diff --git a/src/bignum.h b/src/bignum.h index 743a18fc0f7..a9c7a0a09a8 100644 --- a/src/bignum.h +++ b/src/bignum.h | |||
| @@ -83,7 +83,7 @@ mpz_set_uintmax (mpz_t result, uintmax_t v) | |||
| 83 | /* Return a pointer to an mpz_t that is equal to the Lisp integer I. | 83 | /* Return a pointer to an mpz_t that is equal to the Lisp integer I. |
| 84 | If I is a bignum this returns a pointer to I's representation; | 84 | If I is a bignum this returns a pointer to I's representation; |
| 85 | otherwise this sets *TMP to I's value and returns TMP. */ | 85 | otherwise this sets *TMP to I's value and returns TMP. */ |
| 86 | INLINE mpz_t * | 86 | INLINE mpz_t const * |
| 87 | bignum_integer (mpz_t *tmp, Lisp_Object i) | 87 | bignum_integer (mpz_t *tmp, Lisp_Object i) |
| 88 | { | 88 | { |
| 89 | if (FIXNUMP (i)) | 89 | if (FIXNUMP (i)) |
diff --git a/src/data.c b/src/data.c index 6db8ea144dd..cf9f8e56133 100644 --- a/src/data.c +++ b/src/data.c | |||
| @@ -2871,7 +2871,7 @@ static Lisp_Object | |||
| 2871 | bignum_arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args, | 2871 | bignum_arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args, |
| 2872 | ptrdiff_t argnum, intmax_t iaccum, Lisp_Object val) | 2872 | ptrdiff_t argnum, intmax_t iaccum, Lisp_Object val) |
| 2873 | { | 2873 | { |
| 2874 | mpz_t *accum; | 2874 | mpz_t const *accum; |
| 2875 | if (argnum == 0) | 2875 | if (argnum == 0) |
| 2876 | { | 2876 | { |
| 2877 | accum = bignum_integer (&mpz[0], val); | 2877 | accum = bignum_integer (&mpz[0], val); |
| @@ -2882,7 +2882,7 @@ bignum_arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args, | |||
| 2882 | 2882 | ||
| 2883 | while (true) | 2883 | while (true) |
| 2884 | { | 2884 | { |
| 2885 | mpz_t *next = bignum_integer (&mpz[1], val); | 2885 | mpz_t const *next = bignum_integer (&mpz[1], val); |
| 2886 | 2886 | ||
| 2887 | switch (code) | 2887 | switch (code) |
| 2888 | { | 2888 | { |
| @@ -3099,7 +3099,7 @@ integer_mod (Lisp_Object x, Lisp_Object y) | |||
| 3099 | } | 3099 | } |
| 3100 | else | 3100 | else |
| 3101 | { | 3101 | { |
| 3102 | mpz_t *ym = bignum_integer (&mpz[1], y); | 3102 | mpz_t const *ym = bignum_integer (&mpz[1], y); |
| 3103 | bool neg_y = mpz_sgn (*ym) < 0; | 3103 | bool neg_y = mpz_sgn (*ym) < 0; |
| 3104 | mpz_mod (mpz[0], *bignum_integer (&mpz[0], x), *ym); | 3104 | mpz_mod (mpz[0], *bignum_integer (&mpz[0], x), *ym); |
| 3105 | 3105 | ||
| @@ -3269,7 +3269,7 @@ In this case, the sign bit is duplicated. */) | |||
| 3269 | } | 3269 | } |
| 3270 | } | 3270 | } |
| 3271 | 3271 | ||
| 3272 | mpz_t *zval = bignum_integer (&mpz[0], value); | 3272 | mpz_t const *zval = bignum_integer (&mpz[0], value); |
| 3273 | if (XFIXNUM (count) < 0) | 3273 | if (XFIXNUM (count) < 0) |
| 3274 | { | 3274 | { |
| 3275 | if (TYPE_MAXIMUM (mp_bitcnt_t) < - XFIXNUM (count)) | 3275 | if (TYPE_MAXIMUM (mp_bitcnt_t) < - XFIXNUM (count)) |
diff --git a/src/timefns.c b/src/timefns.c index bf49843aae7..3948f873354 100644 --- a/src/timefns.c +++ b/src/timefns.c | |||
| @@ -423,7 +423,7 @@ decode_float_time (double t, struct lisp_time *result) | |||
| 423 | static Lisp_Object | 423 | static Lisp_Object |
| 424 | ticks_hz_list4 (Lisp_Object ticks, Lisp_Object hz) | 424 | ticks_hz_list4 (Lisp_Object ticks, Lisp_Object hz) |
| 425 | { | 425 | { |
| 426 | mpz_t *zticks = bignum_integer (&mpz[0], ticks); | 426 | mpz_t const *zticks = bignum_integer (&mpz[0], ticks); |
| 427 | #if FASTER_TIMEFNS && TRILLION <= ULONG_MAX | 427 | #if FASTER_TIMEFNS && TRILLION <= ULONG_MAX |
| 428 | mpz_mul_ui (mpz[0], *zticks, TRILLION); | 428 | mpz_mul_ui (mpz[0], *zticks, TRILLION); |
| 429 | #else | 429 | #else |
| @@ -557,8 +557,8 @@ frac_to_double (Lisp_Object numerator, Lisp_Object denominator) | |||
| 557 | 557 | ||
| 558 | verify (FLT_RADIX == 2 || FLT_RADIX == 16); | 558 | verify (FLT_RADIX == 2 || FLT_RADIX == 16); |
| 559 | enum { LOG2_FLT_RADIX = FLT_RADIX == 2 ? 1 : 4 }; | 559 | enum { LOG2_FLT_RADIX = FLT_RADIX == 2 ? 1 : 4 }; |
| 560 | mpz_t *n = bignum_integer (&mpz[0], numerator); | 560 | mpz_t const *n = bignum_integer (&mpz[0], numerator); |
| 561 | mpz_t *d = bignum_integer (&mpz[1], denominator); | 561 | mpz_t const *d = bignum_integer (&mpz[1], denominator); |
| 562 | ptrdiff_t nbits = mpz_sizeinbase (*n, 2); | 562 | ptrdiff_t nbits = mpz_sizeinbase (*n, 2); |
| 563 | ptrdiff_t dbits = mpz_sizeinbase (*d, 2); | 563 | ptrdiff_t dbits = mpz_sizeinbase (*d, 2); |
| 564 | eassume (0 < nbits); | 564 | eassume (0 < nbits); |
| @@ -1061,8 +1061,8 @@ time_arith (Lisp_Object a, Lisp_Object b, bool subtract) | |||
| 1061 | { | 1061 | { |
| 1062 | /* The plan is to decompose ta into na/da and tb into nb/db. | 1062 | /* The plan is to decompose ta into na/da and tb into nb/db. |
| 1063 | Start by computing da and db. */ | 1063 | Start by computing da and db. */ |
| 1064 | mpz_t *da = bignum_integer (&mpz[1], ta.hz); | 1064 | mpz_t const *da = bignum_integer (&mpz[1], ta.hz); |
| 1065 | mpz_t *db = bignum_integer (&mpz[2], tb.hz); | 1065 | mpz_t const *db = bignum_integer (&mpz[2], tb.hz); |
| 1066 | 1066 | ||
| 1067 | /* The plan is to compute (na * (db/g) + nb * (da/g)) / lcm (da, db) | 1067 | /* The plan is to compute (na * (db/g) + nb * (da/g)) / lcm (da, db) |
| 1068 | where g = gcd (da, db). Start by computing g. */ | 1068 | where g = gcd (da, db). Start by computing g. */ |
| @@ -1082,9 +1082,9 @@ time_arith (Lisp_Object a, Lisp_Object b, bool subtract) | |||
| 1082 | 1082 | ||
| 1083 | /* ticks = (fb * na) OPER (fa * nb), where OPER is + or -. | 1083 | /* ticks = (fb * na) OPER (fa * nb), where OPER is + or -. |
| 1084 | OP is the multiply-add or multiply-sub form of OPER. */ | 1084 | OP is the multiply-add or multiply-sub form of OPER. */ |
| 1085 | mpz_t *na = bignum_integer (&mpz[0], ta.ticks); | 1085 | mpz_t const *na = bignum_integer (&mpz[0], ta.ticks); |
| 1086 | mpz_mul (mpz[0], *fb, *na); | 1086 | mpz_mul (mpz[0], *fb, *na); |
| 1087 | mpz_t *nb = bignum_integer (&mpz[3], tb.ticks); | 1087 | mpz_t const *nb = bignum_integer (&mpz[3], tb.ticks); |
| 1088 | (subtract ? mpz_submul : mpz_addmul) (mpz[0], *fa, *nb); | 1088 | (subtract ? mpz_submul : mpz_addmul) (mpz[0], *fa, *nb); |
| 1089 | ticks = make_integer_mpz (); | 1089 | ticks = make_integer_mpz (); |
| 1090 | } | 1090 | } |
| @@ -1144,8 +1144,8 @@ time_cmp (Lisp_Object a, Lisp_Object b) | |||
| 1144 | return 0; | 1144 | return 0; |
| 1145 | 1145 | ||
| 1146 | struct lisp_time tb = lisp_time_struct (b, 0); | 1146 | struct lisp_time tb = lisp_time_struct (b, 0); |
| 1147 | mpz_t *za = bignum_integer (&mpz[0], ta.ticks); | 1147 | mpz_t const *za = bignum_integer (&mpz[0], ta.ticks); |
| 1148 | mpz_t *zb = bignum_integer (&mpz[1], tb.ticks); | 1148 | mpz_t const *zb = bignum_integer (&mpz[1], tb.ticks); |
| 1149 | if (! (FASTER_TIMEFNS && EQ (ta.hz, tb.hz))) | 1149 | if (! (FASTER_TIMEFNS && EQ (ta.hz, tb.hz))) |
| 1150 | { | 1150 | { |
| 1151 | /* This could be sped up by looking at the signs, sizes, and | 1151 | /* This could be sped up by looking at the signs, sizes, and |