tmv: Avoid overflow when byte shift in tmv_to_TimeInterval

Added check so that convertion from a positive tmv not result in a negative TimeInterval and vice versa.

Signed-off-by: Anders Selhammer <anders.selhammer@est.tech>
master
Anders Selhammer 2018-11-13 12:44:40 +00:00 committed by Richard Cochran
parent d663a483c4
commit 14ac29b059
1 changed files with 7 additions and 0 deletions

7
tmv.h
View File

@ -26,6 +26,8 @@
#include "pdt.h" #include "pdt.h"
#define NS_PER_SEC 1000000000LL #define NS_PER_SEC 1000000000LL
#define MIN_TMV_TO_TIMEINTERVAL 0xFFFF800000000000ll
#define MAX_TMV_TO_TIMEINTERVAL 0x00007FFFFFFFFFFFll
/** /**
* We implement the time value as a 64 bit signed integer containing * We implement the time value as a 64 bit signed integer containing
@ -111,6 +113,11 @@ static inline int64_t tmv_to_nanoseconds(tmv_t x)
static inline TimeInterval tmv_to_TimeInterval(tmv_t x) static inline TimeInterval tmv_to_TimeInterval(tmv_t x)
{ {
if (x.ns < (int64_t)MIN_TMV_TO_TIMEINTERVAL) {
return MIN_TMV_TO_TIMEINTERVAL << 16;
} else if (x.ns > (int64_t)MAX_TMV_TO_TIMEINTERVAL) {
return MAX_TMV_TO_TIMEINTERVAL << 16;
}
return x.ns << 16; return x.ns << 16;
} }