From 1e4d38d744917ad7108d877e6b4e95b4a7589230 Mon Sep 17 00:00:00 2001 From: cube0x8 Date: Wed, 9 Oct 2024 18:16:32 +0300 Subject: [PATCH] Fix file sync timing and prevent crash on missing SyncFromDiskMetadata (#2595) * max_time is the current_time(); SyncFromDiskMetadata might not be in state * using metadata_or_insert_with --- libafl/src/stages/sync.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/libafl/src/stages/sync.rs b/libafl/src/stages/sync.rs index 75e53f624f..3dbed175a2 100644 --- a/libafl/src/stages/sync.rs +++ b/libafl/src/stages/sync.rs @@ -102,23 +102,22 @@ where } } - let max_time = match last { - None => None, - Some(last) => Some(last + self.interval), - }; - let new_max_time = max_time.unwrap_or(current_time()); + let new_max_time = current_time(); let mut new_files = vec![]; for dir in &self.sync_dirs { log::debug!("Syncing from dir: {:?}", dir); - let new_dir_files = find_new_files_rec(dir, &max_time)?; + let new_dir_files = find_new_files_rec(dir, &last)?; new_files.extend(new_dir_files); } - *state.metadata_mut::().unwrap() = SyncFromDiskMetadata { - last_time: new_max_time, - left_to_sync: new_files, - }; - let sync_from_disk_metadata = state.metadata_mut::().unwrap(); + + let sync_from_disk_metadata = state + .metadata_or_insert_with(|| SyncFromDiskMetadata::new(new_max_time, new_files.clone())); + + // At the very first sync, last_time and file_to_sync are set twice + sync_from_disk_metadata.last_time = new_max_time; + sync_from_disk_metadata.left_to_sync = new_files; + // Iterate over the paths of files left to sync. // By keeping track of these files, we ensure that no file is missed during synchronization, // even in the event of a target restart.