Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix testcase set_filename #1092

Merged
merged 18 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libafl/src/corpus/ondisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ where
};

let filename_str = filename.to_str().expect("Invalid Path");
testcase.set_filename(filename_str.into());
testcase.set_filename(filename_str.into())?;
};
if self.meta_format.is_some() {
let mut filename = PathBuf::from(testcase.filename().as_ref().unwrap());
Expand Down
32 changes: 31 additions & 1 deletion libafl/src/corpus/testcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use alloc::string::String;
use core::{default::Default, option::Option, time::Duration};

#[cfg(feature = "std")]
use std::{fs, path::Path};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay for testcase.rs to depend on std?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to work without (for inmemory), but for storing to disk it needs std yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added set_Filename function for no_std.

Since there is no file creation/deletion, it just assigns value to struct.


use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -123,8 +126,35 @@ where

/// Set the filename
#[inline]
pub fn set_filename(&mut self, filename: String) {
#[cfg(feature = "std")]
pub fn set_filename(&mut self, filename: String) -> Result<(), Error> {
if self.filename.is_some() {
let f = self.filename.clone().unwrap();
let old_filename = f.as_str();

let new_filename = filename.as_str();

if old_filename.eq(&filename) {
return Ok(());
}

let old_lock_filename = format!(".{old_filename}.lafl_lock");
if Path::new(&old_lock_filename).exists() {
return Err(Error::illegal_state("old lockfile exist"));
}
let new_lock_filename = format!(".{new_filename}.lafl_lock");
if Path::new(&new_lock_filename).exists() {
return Err(Error::illegal_state("new lockfile exist"));
}
fs::rename(old_filename, new_filename)?;

let old_metadata_filename = format!(".{old_filename}.metadata");
let new_metadata_filename = format!(".{new_filename}.metadata");
fs::rename(old_metadata_filename, new_metadata_filename)?;
}

self.filename = Some(filename);
Ok(())
}

/// Get the execution time of the testcase
Expand Down