From da6e67b32192bede9e580cfa46beb49183cee06e Mon Sep 17 00:00:00 2001 From: Stefan Reiter Date: Thu, 1 Oct 2020 11:40:44 +0200 Subject: [PATCH] rrd: fix integer underflow Causes a panic if last_update is smaller than RRD_DATA_ENTRIES*reso, which (I believe) can happen when inserting the first value for a DB. Clamp the value to 0 in that case. Signed-off-by: Stefan Reiter --- src/rrd/rrd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rrd/rrd.rs b/src/rrd/rrd.rs index 8f542b52..86d6b50b 100644 --- a/src/rrd/rrd.rs +++ b/src/rrd/rrd.rs @@ -60,7 +60,7 @@ impl RRA { let min_time = epoch - (RRD_DATA_ENTRIES as u64)*reso; let min_time = (min_time/reso + 1)*reso; - let mut t = last_update - (RRD_DATA_ENTRIES as u64)*reso; + let mut t = last_update.saturating_sub((RRD_DATA_ENTRIES as u64)*reso); let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; for _ in 0..RRD_DATA_ENTRIES { t += reso; index = (index + 1) % RRD_DATA_ENTRIES;