From 5ab2110eb6b4ae9082430081cb2800018cf0dcd6 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sun, 19 Aug 2018 21:03:01 +0300 Subject: Rework predicted_ports_prediction_time_remaining() to fix CID 1438153 --- src/lib/time/tvdiff.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/lib/time/tvdiff.c') diff --git a/src/lib/time/tvdiff.c b/src/lib/time/tvdiff.c index 8617110e52..bc8a1166e7 100644 --- a/src/lib/time/tvdiff.c +++ b/src/lib/time/tvdiff.c @@ -165,3 +165,25 @@ tv_to_msec(const struct timeval *tv) conv += ((int64_t)tv->tv_usec+500)/1000L; return conv; } + +/** + * Return duration in seconds between time_t values + * t1 and t2 iff t1 is numerically + * less or equal than t2. Otherwise, return TIME_MAX. + * + * This provides a safe way to compute difference between + * two UNIX timestamps (t2 can be assumed by calling + * code to be later than t1) or two durations measured + * in seconds (t2 can be assumed to be longer than + * t1). Calling code is expected to check for TIME_MAX + * return value and interpret that as error condition. + */ +time_t +time_diff(const time_t t1, const time_t t2) +{ + if (t1 <= t2) + return t2 - t1; + + return TIME_MAX; +} + -- cgit v1.2.3-54-g00ecf