From ebb1d9da881c31db7ef2db6ae017daa1238134fa Mon Sep 17 00:00:00 2001 From: "Gary E. Miller" Date: Wed, 3 Jun 2015 16:19:42 -0700 Subject: Fix overflow issue on 32 bit binaries. Timespec is a 62 bit number, it does not fit in the 32 bits of a long. Use 'long long' instead. 'long long' is always at least 64 bits long and is the same as a native int when compiled as 64 bit. Note that many 64 bit OS still run 32 bit binaries, and many small devices like RasPi's are 32 bit. --- ppsthread.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ppsthread.h') diff --git a/ppsthread.h b/ppsthread.h index 81faa033..58d4b31a 100644 --- a/ppsthread.h +++ b/ppsthread.h @@ -17,9 +17,9 @@ struct timedelta_t { #endif /* TIMEDELTA_DEFINED */ /* difference between timespecs in nanoseconds */ -/* int is too small, avoid floats */ -/* WARNING! this will overflow if x and y differ by more than a few seconds */ -#define timespec_diff_ns(x, y) (long)(((x).tv_sec-(y).tv_sec)*1000000000+(x).tv_nsec-(y).tv_nsec) +/* int is too small, 32 bit long is too small, avoid floats */ +/* MUST be long long to maintain precision on 32 bit code */ +#define timespec_diff_ns(x, y) (long long)(((x).tv_sec-(y).tv_sec)*1000000000LL+(x).tv_nsec-(y).tv_nsec) /* * Set context, devicefd, and devicename at initialization time, before -- cgit v1.2.1