Skip to content

Commit de1a50e

Browse files
committed
Cleanup checked_sub a bit
1 parent bf0498a commit de1a50e

File tree

14 files changed

+43
-77
lines changed

14 files changed

+43
-77
lines changed

crates/libafl/src/events/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ where
482482
};
483483
let cur = current_time();
484484
// default to 0 here to avoid crashes on clock skew
485-
if cur.checked_sub(*last_report_time).unwrap_or_default() > monitor_timeout {
485+
if cur.saturating_sub(*last_report_time) > monitor_timeout {
486486
// report_progress sets a new `last_report_time` internally.
487487
reporter.report_progress(state)?;
488488
}

crates/libafl/src/executors/hooks/inprocess.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ impl<I, S> HasTimeout for InProcessHooks<I, S> {
154154
null_mut(),
155155
);
156156
}
157-
let elapsed = cur_time
158-
.checked_sub(self.timer().tmout_start_time)
159-
.unwrap_or_default();
157+
let elapsed = cur_time.saturating_sub(self.timer().tmout_start_time);
160158
// set timer the next exec
161159
if self.timer().executions > 0 {
162160
self.timer_mut().avg_exec_time = elapsed / self.timer().executions;
@@ -167,9 +165,7 @@ impl<I, S> HasTimeout for InProcessHooks<I, S> {
167165
return true;
168166
}
169167

170-
let elapsed_run = cur_time
171-
.checked_sub(self.timer_mut().start_time)
172-
.unwrap_or_default();
168+
let elapsed_run = cur_time.saturating_sub(self.timer_mut().start_time);
173169
if elapsed_run < self.timer_mut().exec_tmout {
174170
// fp, reset timeout
175171
unsafe {
@@ -181,9 +177,7 @@ impl<I, S> HasTimeout for InProcessHooks<I, S> {
181177
);
182178
}
183179
if self.timer().executions > 0 {
184-
let elapsed = cur_time
185-
.checked_sub(self.timer_mut().tmout_start_time)
186-
.unwrap_or_default();
180+
let elapsed = cur_time.saturating_sub(self.timer_mut().tmout_start_time);
187181
self.timer_mut().avg_exec_time = elapsed / self.timer().executions;
188182
self.timer_mut().executions = 0; // It will be 1 when the exec finish
189183
}

crates/libafl/src/monitors/disk.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ impl OnDiskTomlMonitor {
127127
{
128128
Self {
129129
filename: filename.into(),
130-
last_update: current_time()
131-
.checked_sub(update_interval)
132-
.unwrap_or_default(),
130+
last_update: current_time().saturating_sub(update_interval),
133131
update_interval,
134132
}
135133
}

crates/libafl/src/monitors/disk_aggregate.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ impl OnDiskJsonAggregateMonitor {
9393
{
9494
Self {
9595
json_path: json_path.into(),
96-
last_update: current_time()
97-
.checked_sub(update_interval)
98-
.unwrap_or_default(),
96+
last_update: current_time().saturating_sub(update_interval),
9997
update_interval,
10098
}
10199
}

crates/libafl/src/monitors/prometheus.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ where
253253
.set(cur_client_clone.execs_per_sec(current_time()));
254254

255255
let client_run_time = current_time()
256-
.checked_sub(cur_client_clone.start_time())
257-
.unwrap_or_default()
256+
.saturating_sub(cur_client_clone.start_time())
258257
.as_secs();
259258
self.prometheus_client_stats
260259
.runtime

crates/libafl/src/monitors/stats/manager.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl ClientStatsManager {
170170

171171
// Time-related data are always re-computed, since it is related with current time.
172172
let cur_time = current_time();
173-
global_stats.run_time = cur_time.checked_sub(self.start_time).unwrap_or_default();
173+
global_stats.run_time = cur_time.saturating_sub(self.start_time);
174174
global_stats.run_time_pretty = format_duration(&global_stats.run_time);
175175
global_stats.execs_per_sec = self
176176
.client_stats
@@ -199,14 +199,11 @@ impl ClientStatsManager {
199199
new_objectives_time = stat.last_objective_time().max(new_objectives_time);
200200
}
201201
if new_path_time > self.start_time() {
202-
total_process_timing.last_new_entry = current_time()
203-
.checked_sub(new_path_time)
204-
.unwrap_or_default();
202+
total_process_timing.last_new_entry = current_time().saturating_sub(new_path_time);
205203
}
206204
if new_objectives_time > self.start_time() {
207-
total_process_timing.last_saved_solution = current_time()
208-
.checked_sub(new_objectives_time)
209-
.unwrap_or_default();
205+
total_process_timing.last_saved_solution =
206+
current_time().saturating_sub(new_objectives_time);
210207
}
211208
}
212209
total_process_timing

crates/libafl/src/monitors/stats/mod.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,7 @@ impl ClientStats {
203203
/// We got new information about executions for this client, insert them.
204204
#[cfg(feature = "afl_exec_sec")]
205205
pub fn update_executions(&mut self, executions: u64, cur_time: Duration) {
206-
let diff = cur_time
207-
.checked_sub(self.last_window_time)
208-
.map_or(0, |d| d.as_secs());
206+
let diff = cur_time.saturating_sub(self.last_window_time).as_secs();
209207
if diff > CLIENT_STATS_TIME_WINDOW_SECS {
210208
let _: f64 = self.execs_per_sec(cur_time);
211209
self.last_window_time = cur_time;
@@ -254,9 +252,7 @@ impl ClientStats {
254252
return 0.0;
255253
}
256254

257-
let elapsed = cur_time
258-
.checked_sub(self.last_window_time)
259-
.map_or(0.0, |d| d.as_secs_f64());
255+
let elapsed = cur_time.saturating_sub(self.last_window_time).as_secs_f64();
260256
if elapsed as u64 == 0 {
261257
return self.last_execs_per_sec;
262258
}
@@ -286,9 +282,7 @@ impl ClientStats {
286282
return 0.0;
287283
}
288284

289-
let elapsed = cur_time
290-
.checked_sub(self.last_window_time)
291-
.map_or(0.0, |d| d.as_secs_f64());
285+
let elapsed = cur_time.saturating_sub(self.last_window_time).as_secs_f64();
292286
if elapsed as u64 == 0 {
293287
return 0.0;
294288
}
@@ -330,17 +324,13 @@ impl ClientStats {
330324
pub fn process_timing(&mut self) -> ProcessTiming {
331325
let client_start_time = self.start_time();
332326
let last_new_entry = if self.last_corpus_time() > self.start_time() {
333-
current_time()
334-
.checked_sub(self.last_corpus_time())
335-
.unwrap_or_default()
327+
current_time().saturating_sub(self.last_corpus_time())
336328
} else {
337329
Duration::default()
338330
};
339331

340332
let last_saved_solution = if self.last_objective_time() > self.start_time() {
341-
current_time()
342-
.checked_sub(self.last_objective_time())
343-
.unwrap_or_default()
333+
current_time().saturating_sub(self.last_objective_time())
344334
} else {
345335
Duration::default()
346336
};

crates/libafl/src/monitors/tui/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ impl TimedStats {
137137
.back()
138138
.unwrap_or(&default_stat)
139139
.time
140-
.checked_sub(self.series.front().unwrap_or(&default_stat).time)
141-
.unwrap()
140+
.saturating_sub(self.series.front().unwrap_or(&default_stat).time)
141+
.unwrap_or(window)
142142
>= window
143143
{
144144
self.series.pop_front();
@@ -562,9 +562,7 @@ fn run_tui_thread<W: Write + Send + Sync + 'static>(
562562
}
563563
terminal.draw(|f| ui.draw(f, &context))?;
564564

565-
let timeout = tick_rate
566-
.checked_sub(last_tick.elapsed())
567-
.unwrap_or_else(|| Duration::from_secs(0));
565+
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
568566
if event::poll(timeout)?
569567
&& let Event::Key(key) = event::read()?
570568
{

crates/libafl/src/monitors/tui/ui.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl TuiUi {
319319
let start = stats.series.front().unwrap().time;
320320
let end = stats.series.back().unwrap().time;
321321
let min_lbl_x = format_duration(&start);
322-
let med_lbl_x = format_duration(&(end.checked_sub(start).unwrap_or_default() / 2));
322+
let med_lbl_x = format_duration(&(end.saturating_sub(start) / 2));
323323
let max_lbl_x = format_duration(&end);
324324

325325
let x_labels = vec![
@@ -329,7 +329,7 @@ impl TuiUi {
329329
];
330330

331331
let max_x = u64::from(area.width);
332-
let window = end.checked_sub(start).unwrap_or_default();
332+
let window = end.saturating_sub(start);
333333
let time_unit = if max_x > window.as_secs() {
334334
0 // millis / 10
335335
} else if max_x > window.as_secs() * 60 {
@@ -539,7 +539,7 @@ impl TuiUi {
539539
Row::new(vec![
540540
Cell::from(Span::raw("run time")),
541541
Cell::from(Span::raw(format_duration(
542-
&current_time().checked_sub(tup.0).unwrap_or_default(),
542+
&current_time().saturating_sub(tup.0),
543543
))),
544544
]),
545545
Row::new(vec![

crates/libafl/src/mutators/mutations.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,8 +1971,7 @@ mod tests {
19711971
assert!(counts.into_iter().all(|count| {
19721972
count
19731973
.checked_sub(average)
1974-
.or_else(|| average.checked_sub(count))
1975-
.unwrap()
1974+
.unwrap_or_else(|| average.saturating_sub(count))
19761975
< 500
19771976
}));
19781977
Ok(())
@@ -2021,8 +2020,7 @@ mod tests {
20212020
assert!(counts.into_iter().all(|count| {
20222021
count
20232022
.checked_sub(average)
2024-
.or_else(|| average.checked_sub(count))
2025-
.unwrap()
2023+
.unwrap_or_else(|| average.saturating_sub(count))
20262024
< 500
20272025
}));
20282026
Ok(())
@@ -2067,16 +2065,14 @@ mod tests {
20672065
assert!(counts.into_iter().all(|count| {
20682066
count
20692067
.checked_sub(average)
2070-
.or_else(|| average.checked_sub(count))
2071-
.unwrap()
2068+
.unwrap_or_else(|| average.saturating_sub(count))
20722069
< 500
20732070
}));
20742071
let average = insertions.iter().copied().sum::<usize>() / insertions.len();
20752072
assert!(insertions.into_iter().all(|count| {
20762073
count
20772074
.checked_sub(average)
2078-
.or_else(|| average.checked_sub(count))
2079-
.unwrap()
2075+
.unwrap_or_else(|| average.saturating_sub(count))
20802076
< 500
20812077
}));
20822078
Ok(())
@@ -2124,16 +2120,14 @@ mod tests {
21242120
assert!(counts.iter().all(|&count| {
21252121
count
21262122
.checked_sub(average)
2127-
.or_else(|| average.checked_sub(count))
2128-
.unwrap()
2123+
.unwrap_or_else(|| average.saturating_sub(count))
21292124
< 500
21302125
}),);
21312126
let average = insertions.iter().copied().sum::<usize>() / insertions.len();
21322127
assert!(insertions.into_iter().all(|count| {
21332128
count
21342129
.checked_sub(average)
2135-
.or_else(|| average.checked_sub(count))
2136-
.unwrap()
2130+
.unwrap_or_else(|| average.saturating_sub(count))
21372131
< 500
21382132
}));
21392133
Ok(())

0 commit comments

Comments
 (0)