Skip to content

Commit

Permalink
Improve use of anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Jul 19, 2024
1 parent 6afc76e commit 1ba9f20
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions cargo-afl/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![deny(clippy::disallowed_macros, clippy::expect_used, clippy::unwrap_used)]

use anyhow::{anyhow, Context, Result};
use anyhow::{bail, ensure, Context, Result};
use clap::Parser;
use std::ffi::OsStr;
use std::path::Path;
Expand Down Expand Up @@ -40,10 +40,10 @@ pub fn config(args: &Args) -> Result<()> {
let archive_file_path = common::archive_file_path()?;
if !args.force && archive_file_path.exists() && args.plugins == common::plugins_available()? {
let version = common::afl_rustc_version()?;
return Err(anyhow!(
bail!(
"AFL LLVM runtime was already built for Rust {version}; run `cargo afl config --build \
--force` to rebuild it."
));
);
}

let afl_src_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join(AFL_SRC_PATH);
Expand All @@ -57,9 +57,7 @@ pub fn config(args: &Args) -> Result<()> {
.status()
.as_ref()
.map_or(false, ExitStatus::success);
if !success {
return Err(anyhow!("could not run 'git'"));
}
ensure!(success, "could not run 'git'");
} else {
let _: u64 = fs_extra::dir::copy(
afl_src_dir,
Expand All @@ -82,7 +80,7 @@ pub fn config(args: &Args) -> Result<()> {

let afl_dir = common::afl_dir()?;
let Some(dir) = afl_dir.parent().map(Path::to_path_buf) else {
return Err(anyhow!("could not get afl dir parent"));
bail!("could not get afl dir parent");
};
eprintln!("Artifacts written to {}", dir.display());

Expand Down Expand Up @@ -119,9 +117,7 @@ fn build_afl(args: &Args, work_dir: &Path) -> Result<()> {
}

let success = command.status().as_ref().map_or(false, ExitStatus::success);
if !success {
return Err(anyhow!("could not run 'make install'"));
}
ensure!(success, "could not run 'make install'");

Ok(())
}
Expand All @@ -144,9 +140,7 @@ fn build_afl_llvm_runtime(args: &Args, work_dir: &Path) -> Result<()> {
}

let success = command.status().as_ref().map_or(false, ExitStatus::success);
if !success {
return Err(anyhow!("could not run 'ar'"));
}
ensure!(success, "could not run 'ar'");

Ok(())
}
Expand Down Expand Up @@ -176,15 +170,13 @@ fn check_llvm_and_get_config() -> Result<String> {
// Make sure we are on nightly for the -Z flags
let version_meta = rustc_version::version_meta()?;
if version_meta.channel != rustc_version::Channel::Nightly {
return Err(anyhow!(
"cargo-afl must be compiled with nightly for the plugins feature",
));
bail!("cargo-afl must be compiled with nightly for the plugins feature",);
}
let Some(llvm_version) = version_meta
.llvm_version
.map(|llvm_version| llvm_version.major.to_string())
else {
return Err(anyhow!("could not get llvm version"));
bail!("could not get llvm version");
};

// Fetch the llvm version of the rust toolchain and set the LLVM_CONFIG environment variable to the same version
Expand All @@ -205,15 +197,13 @@ fn check_llvm_and_get_config() -> Result<String> {
let version = String::from_utf8(out.stdout)
.with_context(|| format!("could not convert {llvm_config} --version output to utf8"))?;
let Some(major) = version.split('.').next() else {
return Err(anyhow!(
"could not get major from {llvm_config} --version output",
));
bail!("could not get major from {llvm_config} --version output",);
};
if major != llvm_version {
return Err(anyhow!(
bail!(
"{llvm_config} --version output does not contain expected major version \
({llvm_version})",
));
);
}

Ok(llvm_config)
Expand Down

0 comments on commit 1ba9f20

Please sign in to comment.