Skip to content

Commit 00b698c

Browse files
author
Mark Stalzer
committed
variance_measure_add: skip non-finite input instead of asserting
An assert(isfinite(d[i])) crash here corrupts the on-disk libsurvive config because the process dies mid-write. Corrupt optical angles (e.g. bad FPGA timestamps during USB disturbances) can produce NaN or Inf values that reach this function; crashing is strictly worse than dropping one sample, which has negligible effect on the variance estimate. Replace the assert with an early-return guard that logs to stderr and leaves the accumulator unchanged.
1 parent eb409fc commit 00b698c

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

redist/variance.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@ static inline void variance_measure_add(struct variance_measure *meas, const FLT
1212
if (meas->size == 0)
1313
meas->size = 1;
1414

15+
/* Guard against non-finite values. Corrupt optical angles (e.g. bad FPGA
16+
* timestamps during USB disturbances) can reach here as NaN or Inf. The
17+
* assert below would crash the process and corrupt the on-disk config;
18+
* dropping one sample is strictly safer and has negligible effect on the
19+
* variance estimate. */
20+
for (int i = 0; i < meas->size; i++) {
21+
if (!isfinite(d[i])) {
22+
fprintf(stderr, "[libsurvive] variance_measure_add: non-finite d[%d]=%f, dropping measurement\n", i, (double)d[i]);
23+
return;
24+
}
25+
}
26+
1527
meas->n++;
1628
addnd(meas->sum, meas->sum, d, meas->size);
1729
for (int i = 0; i < meas->size; i++) {
18-
assert(isfinite(d[i]));
1930
meas->sumSq[i] += d[i] * d[i];
2031
}
2132
}

0 commit comments

Comments
 (0)