From: Florian Forster Date: Tue, 8 Dec 2015 09:33:17 +0000 (+0100) Subject: src/daemon/utils_time.h: Avoid incorrect rounding in CDTIME_T_TO_TIME{VAL,SPEC}. X-Git-Tag: collectd-5.6.0~511 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=0a62e9b75d84c39f57cfdf0e80710627fa9091cb;p=collectd.git src/daemon/utils_time.h: Avoid incorrect rounding in CDTIME_T_TO_TIME{VAL,SPEC}. This change also partially expands CDTIME_T_TO_{US,NS} to avoid dead code warnings in Coverity. Fixes: #1446 CID: 37950, 37951, 37952, 37953, 37954, 41722 --- diff --git a/src/daemon/utils_time.h b/src/daemon/utils_time.h index 9e980406..46faa9ce 100644 --- a/src/daemon/utils_time.h +++ b/src/daemon/utils_time.h @@ -68,15 +68,15 @@ extern cdtime_t cdtime_mock; #define CDTIME_T_TO_DOUBLE(t) (((double) (t)) / 1073741824.0) #define DOUBLE_TO_CDTIME_T(d) ((cdtime_t) ((d) * 1073741824.0)) -#define CDTIME_T_TO_TIMEVAL(cdt,tvp) do { \ - (tvp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ - (tvp)->tv_usec = (suseconds_t) CDTIME_T_TO_US ((cdt) & 0x3fffffff); \ +#define CDTIME_T_TO_TIMEVAL(cdt,tvp) do { \ + (tvp)->tv_sec = (time_t) ((cdt) >> 30); \ + (tvp)->tv_usec = (suseconds_t) ((((cdt) & 0x3fffffff) * 1000000 + (1 << 29)) >> 30); \ } while (0) #define TIMEVAL_TO_CDTIME_T(tv) US_TO_CDTIME_T(1000000 * (tv)->tv_sec + (tv)->tv_usec) -#define CDTIME_T_TO_TIMESPEC(cdt,tsp) do { \ - (tsp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ - (tsp)->tv_nsec = (long) CDTIME_T_TO_NS ((cdt) & 0x3fffffff); \ +#define CDTIME_T_TO_TIMESPEC(cdt,tsp) do { \ + (tsp)->tv_sec = (time_t) ((cdt) >> 30); \ + (tsp)->tv_nsec = (long) ((((cdt) & 0x3fffffff) * 1000000000 + (1 << 29)) >> 30); \ } while (0) #define TIMESPEC_TO_CDTIME_T(ts) NS_TO_CDTIME_T(1000000000ULL * (ts)->tv_sec + (ts)->tv_nsec)