aboutsummaryrefslogtreecommitdiffstats
path: root/src/timefns.c
diff options
context:
space:
mode:
authorPaul Eggert2019-08-21 00:06:00 -0700
committerPaul Eggert2019-08-21 00:11:45 -0700
commit39fee209942ab7c35b4789f0010264cd6a52197b (patch)
tree96f1858c890436713ba0da0fca93d1f33d7dd33a /src/timefns.c
parent3881542edeac3e94291c2ce574edf0b0e52764a8 (diff)
downloademacs-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/timefns.c')
-rw-r--r--src/timefns.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/timefns.c b/src/timefns.c
index 3c4c15b6576..6c9473f22a6 100644
--- a/src/timefns.c
+++ b/src/timefns.c
@@ -91,7 +91,7 @@ static Lisp_Object timespec_hz;
91#define TRILLION 1000000000000 91#define TRILLION 1000000000000
92#if FIXNUM_OVERFLOW_P (TRILLION) 92#if FIXNUM_OVERFLOW_P (TRILLION)
93static Lisp_Object trillion; 93static Lisp_Object trillion;
94# define ztrillion (XBIGNUM (trillion)->value) 94# define ztrillion (*xbignum_val (trillion))
95#else 95#else
96# define trillion make_fixnum (TRILLION) 96# define trillion make_fixnum (TRILLION)
97# if ULONG_MAX < TRILLION || !FASTER_TIMEFNS 97# if ULONG_MAX < TRILLION || !FASTER_TIMEFNS
@@ -534,7 +534,7 @@ lisp_time_hz_ticks (struct lisp_time t, Lisp_Object hz)
534 return make_int (ticks / XFIXNUM (t.hz) 534 return make_int (ticks / XFIXNUM (t.hz)
535 - (ticks % XFIXNUM (t.hz) < 0)); 535 - (ticks % XFIXNUM (t.hz) < 0));
536 } 536 }
537 else if (! (BIGNUMP (hz) && 0 < mpz_sgn (XBIGNUM (hz)->value))) 537 else if (! (BIGNUMP (hz) && 0 < mpz_sgn (*xbignum_val (hz))))
538 invalid_hz (hz); 538 invalid_hz (hz);
539 539
540 mpz_mul (mpz[0], 540 mpz_mul (mpz[0],
@@ -906,6 +906,7 @@ lisp_to_timespec (struct lisp_time t)
906 struct timespec result = invalid_timespec (); 906 struct timespec result = invalid_timespec ();
907 int ns; 907 int ns;
908 mpz_t *q = &mpz[0]; 908 mpz_t *q = &mpz[0];
909 mpz_t const *qt = q;
909 910
910 if (FASTER_TIMEFNS && EQ (t.hz, timespec_hz)) 911 if (FASTER_TIMEFNS && EQ (t.hz, timespec_hz))
911 { 912 {
@@ -924,7 +925,7 @@ lisp_to_timespec (struct lisp_time t)
924 return result; 925 return result;
925 } 926 }
926 else 927 else
927 ns = mpz_fdiv_q_ui (*q, XBIGNUM (t.ticks)->value, TIMESPEC_HZ); 928 ns = mpz_fdiv_q_ui (*q, *xbignum_val (t.ticks), TIMESPEC_HZ);
928 } 929 }
929 else if (FASTER_TIMEFNS && EQ (t.hz, make_fixnum (1))) 930 else if (FASTER_TIMEFNS && EQ (t.hz, make_fixnum (1)))
930 { 931 {
@@ -941,7 +942,7 @@ lisp_to_timespec (struct lisp_time t)
941 return result; 942 return result;
942 } 943 }
943 else 944 else
944 q = &XBIGNUM (t.ticks)->value; 945 qt = xbignum_val (t.ticks);
945 } 946 }
946 else 947 else
947 { 948 {
@@ -953,7 +954,7 @@ lisp_to_timespec (struct lisp_time t)
953 /* With some versions of MinGW, tv_sec is a 64-bit type, whereas 954 /* With some versions of MinGW, tv_sec is a 64-bit type, whereas
954 time_t is a 32-bit type. */ 955 time_t is a 32-bit type. */
955 time_t sec; 956 time_t sec;
956 if (mpz_time (*q, &sec)) 957 if (mpz_time (*qt, &sec))
957 { 958 {
958 result.tv_sec = sec; 959 result.tv_sec = sec;
959 result.tv_nsec = ns; 960 result.tv_nsec = ns;
@@ -1038,7 +1039,7 @@ lispint_arith (Lisp_Object a, Lisp_Object b, bool subtract)
1038 if (eabs (XFIXNUM (b)) <= ULONG_MAX) 1039 if (eabs (XFIXNUM (b)) <= ULONG_MAX)
1039 { 1040 {
1040 ((XFIXNUM (b) < 0) == subtract ? mpz_add_ui : mpz_sub_ui) 1041 ((XFIXNUM (b) < 0) == subtract ? mpz_add_ui : mpz_sub_ui)
1041 (mpz[0], XBIGNUM (a)->value, eabs (XFIXNUM (b))); 1042 (mpz[0], *xbignum_val (a), eabs (XFIXNUM (b)));
1042 mpz_done = true; 1043 mpz_done = true;
1043 } 1044 }
1044 } 1045 }