sourcestats: check samples loaded from dump files

When loading a dump file with the -r option, check also sanity of the
sample time, offset, peer/root delay/dispersion, and the sample order to
better handle corrupted files.
This commit is contained in:
Miroslav Lichvar 2021-05-04 14:06:33 +02:00
parent bb9ba3e4bd
commit fc99317291

View file

@ -885,6 +885,7 @@ int
SST_LoadFromFile(SST_Stats inst, FILE *in)
{
int i, n_samples, arun;
struct timespec now;
double sample_time;
char line[256];
@ -895,6 +896,8 @@ SST_LoadFromFile(SST_Stats inst, FILE *in)
SST_ResetInstance(inst);
LCL_ReadCookedTime(&now, NULL);
for (i = 0; i < n_samples; i++) {
if (!fgets(line, sizeof (line), in) ||
sscanf(line, "%lf %lf %lf %lf %lf %lf %lf",
@ -903,8 +906,19 @@ SST_LoadFromFile(SST_Stats inst, FILE *in)
&inst->root_delays[i], &inst->root_dispersions[i]) != 7)
return 0;
if (!UTI_IsTimeOffsetSane(&now, sample_time - UTI_TimespecToDouble(&now)))
return 0;
/* Some resolution is lost in the double format, but that's ok */
UTI_DoubleToTimespec(sample_time, &inst->sample_times[i]);
/* Make sure the samples are sane and they are in order */
if (!UTI_IsTimeOffsetSane(&inst->sample_times[i], -inst->offsets[i]) ||
!(fabs(inst->peer_delays[i]) < 1.0e6 && fabs(inst->peer_dispersions[i]) < 1.0e6 &&
fabs(inst->root_delays[i]) < 1.0e6 && fabs(inst->root_dispersions[i]) < 1.0e6) ||
(i > 0 && UTI_CompareTimespecs(&inst->sample_times[i],
&inst->sample_times[i - 1]) <= 0))
return 0;
}
inst->n_samples = n_samples;