From 0a848e2528aaef0b3347de0b49ce50da8dc1c9a4 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 6 Oct 2016 15:21:43 +0200 Subject: [PATCH] refclock: require new samples to have newer timestamp If all or most SHM/SOCK samples collected in a polling interval had the same local timestamp, the dispersion could end up as nan, which could trigger an assert failure later in the code. Before accumulating a refclock sample, check if the timestamp is newer than the previous one. --- refclock.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/refclock.c b/refclock.c index 7ee1468..4439d9c 100644 --- a/refclock.c +++ b/refclock.c @@ -505,16 +505,22 @@ RCL_AddPulse(RCL_Instance instance, struct timeval *pulse_time, double second) static int valid_sample_time(RCL_Instance instance, struct timeval *tv) { - struct timeval raw_time; - double diff; + struct timeval raw_time, last_sample_time; + double diff, last_offset, last_dispersion; LCL_ReadRawTime(&raw_time); UTI_DiffTimevalsToDouble(&diff, &raw_time, tv); - if (diff < 0.0 || diff > UTI_Log2ToDouble(instance->poll + 1)) { - DEBUG_LOG(LOGF_Refclock, "%s refclock sample not valid age=%.6f tv=%s", - UTI_RefidToString(instance->ref_id), diff, UTI_TimevalToString(tv)); + + if (diff < 0.0 || diff > UTI_Log2ToDouble(instance->poll + 1) || + (filter_get_last_sample(&instance->filter, &last_sample_time, + &last_offset, &last_dispersion) && + UTI_CompareTimevals(&last_sample_time, tv) >= 0)) { + DEBUG_LOG(LOGF_Refclock, "%s refclock sample not valid age=%.6f ts=%s lastts=%s", + UTI_RefidToString(instance->ref_id), diff, + UTI_TimevalToString(tv), UTI_TimevalToString(&last_sample_time)); return 0; } + return 1; }