From f9715392af00bc1419dc54b203810d41cc430305 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Fri, 14 Feb 2025 02:07:14 +0100 Subject: [PATCH] Try to solve corpus issue related to #2981 (#2982) * Try to solve corpus issue related to #2981 * clippy --- libafl/src/corpus/inmemory_ondisk.rs | 30 +++++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/libafl/src/corpus/inmemory_ondisk.rs b/libafl/src/corpus/inmemory_ondisk.rs index 1e8d469bba..690bd82a58 100644 --- a/libafl/src/corpus/inmemory_ondisk.rs +++ b/libafl/src/corpus/inmemory_ondisk.rs @@ -362,7 +362,7 @@ impl InMemoryOnDiskCorpus { testcase.input().as_ref().unwrap().generate_name(id) }); - let mut ctr = String::new(); + let mut ctr = 1; if self.locking { let lockfile_name = format!(".{file_name}"); let lockfile_path = self.dir_path.join(lockfile_name); @@ -375,15 +375,14 @@ impl InMemoryOnDiskCorpus { ); lockfile.lock_exclusive()?; - lockfile.read_to_string(&mut ctr)?; - ctr = if ctr.is_empty() { - String::from("1") - } else { - (ctr.trim().parse::()? + 1).to_string() - }; + let mut old_ctr = String::new(); + lockfile.read_to_string(&mut old_ctr)?; + if !old_ctr.is_empty() { + ctr = old_ctr.trim().parse::()? + 1; + } lockfile.seek(SeekFrom::Start(0))?; - lockfile.write_all(ctr.as_bytes())?; + lockfile.write_all(ctr.to_string().as_bytes())?; } if testcase.file_path().is_none() { @@ -432,11 +431,18 @@ impl InMemoryOnDiskCorpus { *testcase.metadata_path_mut() = Some(metafile_path); } - if let Err(err) = self.store_input_from(testcase) { - if self.locking { - return Err(err); + // Only try to write the data if the counter is 1. + // Otherwise we already have a file with this name, and + // we can assume the data has already been written. + if ctr == 1 { + if let Err(err) = self.store_input_from(testcase) { + if self.locking { + return Err(err); + } + log::error!( + "An error occurred when trying to write a testcase without locking: {err}" + ); } - log::error!("An error occurred when trying to write a testcase without locking: {err}"); } Ok(()) }