drv_hrt: robustify wrap protection with half the range of the 64 bit timestamp

This disallows any timespans longer than 292k years instead of disallowing
any negative timespans which can occur and be processed correctly when the timestamp wraps e.g. because a timestamp is calculated by subtracting more
time than already passed since boot (or after 584k years).
This commit is contained in:
Matthias Grob
2024-07-10 14:52:09 +02:00
parent 3fe609f769
commit a0e58837e1
+5 -4
View File
@@ -164,10 +164,11 @@ static inline hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
{
hrt_abstime now = hrt_absolute_time();
// Cannot allow a negative elapsed time as this would appear
// to be a huge positive elapsed time when represented as an
// unsigned value!
if (*then > now) {
// Zero out time differences bigger than half the timestamp range (~292k years)
// because this is indicating an unwanted wrap of the unsigned timestamp
// and hence a negative time difference (*then lies in the future).
if ((now - *then) > ((uint64_t)(-1) >> 1)) {
return 0;
}