mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-16 11:47:35 +08:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user