Skip to content

Commit

Permalink
clippy lint fixes (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdea authored Nov 18, 2024
1 parent 932d8f2 commit efe6584
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 104 deletions.
4 changes: 1 addition & 3 deletions GITHUB-ACTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ hosting/generation. To help downstream consumers of idalib avoid headaches, we
have documented how we've setup GitHub Actions for idalib to perform a build a
viability test on each supported OS, and to generate and deploy documentation.


## Prerequisites

To adapt the workflows in this document in your own project, you need to
configure two repository secrets via Settings -> Secrets and variables ->
Actions:

- `IDASDK90_URL`: a publicly accessible encrypted/password protected archive
- `IDASDK90_URL`: a publicly accessible encrypted/password-protected archive
containing he latest IDA SDK. We are using a zip in our workflow below, but
it should be straightforward to use an approach like
[binexport](https://github.com/google/binexport/blob/23619ba62d88b3b93615d28fe3033489d12b38ac/.github/workflows/cmake.yml#L25),
Expand Down Expand Up @@ -130,7 +129,6 @@ a branch to keep the documentation separate from the source code, e.g.,
GitHub Pages needs to be configured via Settings -> Pages by setting "Source"
to "Deploy from a branch" and "branch" to "gh-pages".


```yml
name: document

Expand Down
4 changes: 1 addition & 3 deletions idalib-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ pub fn idalib_install_paths() -> (PathBuf, PathBuf, PathBuf) {
}

pub fn idalib_install_paths_with(check: bool) -> (PathBuf, PathBuf, PathBuf) {
let path = env::var("IDADIR")
.map(PathBuf::from)
.unwrap_or_else(|_| link_path());
let path = env::var("IDADIR").map_or_else(|_| link_path(), PathBuf::from);

let (idalib, ida) = if cfg!(target_os = "linux") {
(path.join("libidalib.so"), path.join("libida.so"))
Expand Down
10 changes: 7 additions & 3 deletions idalib-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {

let ffi_path = PathBuf::from("src");

let mut builder = autocxx_build::Builder::new(ffi_path.join("lib.rs"), &[&ffi_path, &ida])
let mut builder = autocxx_build::Builder::new(ffi_path.join("lib.rs"), [&ffi_path, &ida])
.extra_clang_args(
#[cfg(target_os = "linux")]
&["-std=c++17", "-D__LINUX__=1", "-D__EA64__=1"],
Expand Down Expand Up @@ -125,7 +125,7 @@ fn main() {
("MIPS_.*", "insn_mips.rs"),
];

for (prefix, output) in insn_consts.into_iter() {
for (prefix, output) in insn_consts {
let arch = autocxx_bindgen::builder()
.header(ida.join("pro.h").to_str().expect("path is valid string"))
.header(
Expand All @@ -141,7 +141,11 @@ fn main() {

let hexrays = autocxx_bindgen::builder()
.header(ida.join("pro.h").to_str().expect("path is valid string"))
.header(ida.join("hexrays.hpp").to_str().expect("path is valid string"))
.header(
ida.join("hexrays.hpp")
.to_str()
.expect("path is valid string"),
)
.opaque_type("std::.*")
.opaque_type("carglist_t")
.allowlist_item("cfunc_t")
Expand Down
90 changes: 50 additions & 40 deletions idalib-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub enum IDAError {
#[error(transparent)]
Ffi(anyhow::Error),
#[error("could not initialise IDA: error code {:x}", _0.0)]
Init(autocxx::c_int),
Init(c_int),
#[error("could not open IDA database: error code {:x}", _0.0)]
OpenDb(autocxx::c_int),
OpenDb(c_int),
#[error("could not close IDA database: error code {:x}", _0.0)]
CloseDb(autocxx::c_int),
CloseDb(c_int),
#[error("invalid license")]
InvalidLicense,
#[error("could not generate pattern or signature files")]
Expand Down Expand Up @@ -965,20 +965,22 @@ pub mod ida {
pub use ffi::auto_wait;

pub fn is_license_valid() -> bool {
if !is_main_thread() {
panic!("IDA cannot function correctly when not running on the main thread");
}
assert!(
is_main_thread(),
"IDA cannot function correctly when not running on the main thread"
);

unsafe { self::ffix::idalib_check_license() }
unsafe { ffix::idalib_check_license() }
}

pub fn license_id() -> Result<[u8; 6], IDAError> {
if !is_main_thread() {
panic!("IDA cannot function correctly when not running on the main thread");
}
assert!(
is_main_thread(),
"IDA cannot function correctly when not running on the main thread"
);

let mut lid = [0u8; 6];
if unsafe { self::ffix::idalib_get_license_id(&mut lid) } {
if unsafe { ffix::idalib_get_license_id(&mut lid) } {
Ok(lid)
} else {
Err(IDAError::InvalidLicense)
Expand All @@ -987,13 +989,14 @@ pub mod ida {

// NOTE: once; main thread
pub fn init_library() -> Result<(), IDAError> {
if !is_main_thread() {
panic!("IDA cannot function correctly when not running on the main thread");
}
assert!(
is_main_thread(),
"IDA cannot function correctly when not running on the main thread"
);

env::set_var("TVHEADLESS", "1");

let res = unsafe { self::ffix::init_library(c_int(0), ptr::null_mut()) };
let res = unsafe { ffix::init_library(c_int(0), ptr::null_mut()) };

if res != c_int(0) {
Err(IDAError::Init(res))
Expand All @@ -1003,30 +1006,34 @@ pub mod ida {
}

pub fn make_signatures(only_pat: bool) -> Result<(), IDAError> {
if !is_main_thread() {
panic!("IDA cannot function correctly when not running on the main thread");
}
assert!(
is_main_thread(),
"IDA cannot function correctly when not running on the main thread"
);

if unsafe { self::ffi::make_signatures(only_pat) } {
if unsafe { ffi::make_signatures(only_pat) } {
Ok(())
} else {
Err(IDAError::MakeSigs)
}
}

pub fn enable_console_messages(enable: bool) {
if !is_main_thread() {
panic!("IDA cannot function correctly when not running on the main thread");
}
assert!(
is_main_thread(),
"IDA cannot function correctly when not running on the main thread"
);

unsafe { self::ffi::enable_console_messages(enable) }
unsafe { ffi::enable_console_messages(enable) }
}

pub fn set_screen_ea(ea: ea_t) {
if !is_main_thread() {
panic!("IDA cannot function correctly when not running on the main thread");
}
unsafe { self::ffi::set_screen_ea(ea) }
assert!(
is_main_thread(),
"IDA cannot function correctly when not running on the main thread"
);

unsafe { ffi::set_screen_ea(ea) }
}

pub fn open_database(path: impl AsRef<Path>) -> Result<(), IDAError> {
Expand All @@ -1035,17 +1042,18 @@ pub mod ida {

// NOTE: main thread
pub fn open_database_with(path: impl AsRef<Path>, auto_analysis: bool) -> Result<(), IDAError> {
if !is_main_thread() {
panic!("IDA cannot function correctly when not running on the main thread");
}
assert!(
is_main_thread(),
"IDA cannot function correctly when not running on the main thread"
);

if !is_license_valid() {
return Err(IDAError::InvalidLicense);
}

let path = CString::new(path.as_ref().to_string_lossy().as_ref()).map_err(IDAError::ffi)?;

let res = unsafe { self::ffi::open_database(path.as_ptr(), auto_analysis) };
let res = unsafe { ffi::open_database(path.as_ptr(), auto_analysis) };

if res != c_int(0) {
Err(IDAError::OpenDb(res))
Expand All @@ -1058,17 +1066,18 @@ pub mod ida {
path: impl AsRef<Path>,
auto_analysis: bool,
) -> Result<(), IDAError> {
if !is_main_thread() {
panic!("IDA cannot function correctly when not running on the main thread");
}
assert!(
is_main_thread(),
"IDA cannot function correctly when not running on the main thread"
);

if !is_license_valid() {
return Err(IDAError::InvalidLicense);
}

let path = CString::new(path.as_ref().to_string_lossy().as_ref()).map_err(IDAError::ffi)?;

let res = unsafe { self::ffix::idalib_open_database_quiet(path.as_ptr(), auto_analysis) };
let res = unsafe { ffix::idalib_open_database_quiet(path.as_ptr(), auto_analysis) };

if res != c_int(0) {
Err(IDAError::OpenDb(res))
Expand All @@ -1078,14 +1087,15 @@ pub mod ida {
}

pub fn close_database() {
close_database_with(true)
close_database_with(true);
}

pub fn close_database_with(save: bool) {
if !is_main_thread() {
panic!("IDA cannot function correctly when not running on the main thread");
}
assert!(
is_main_thread(),
"IDA cannot function correctly when not running on the main thread"
);

unsafe { self::ffi::close_database(save) }
unsafe { ffi::close_database(save) }
}
}
2 changes: 1 addition & 1 deletion idalib-sys/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main_thread_id() -> u32 {

static mut MAIN_THREAD_ID: u32 = 0;

/// Function pointer used in CRT initialization section to set the above static field's value.
// Function pointer used in CRT initialization section to set the above static field's value.

// Mark as used so this is not removable.
#[used]
Expand Down
6 changes: 3 additions & 3 deletions idalib/examples/decrypt_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ struct EncString {

impl EncString {
fn set_address(&mut self, address: u64) {
self.address = Some(address)
self.address = Some(address);
}

fn set_size(&mut self, size: usize) {
self.size = Some(size)
self.size = Some(size);
}

fn ready(self) -> bool {
Expand Down Expand Up @@ -52,7 +52,7 @@ impl EncString {

for i in 0..size {
let kbyte = Self::shrink_byte(enc[2 * (size - i - 1)]);
dec[size - i - 1] = kbyte ^ dec[size - i]
dec[size - i - 1] = kbyte ^ dec[size - i];
}

for i in (0..size - 1).step_by(2) {
Expand Down
8 changes: 6 additions & 2 deletions idalib/examples/dump_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ fn main() -> anyhow::Result<()> {
);

if let Some(df) = decompiled.as_ref() {
println!("statements: {} / {}", df.body().len(), df.body().iter().count());
println!(
"statements: {} / {}",
df.body().len(),
df.body().iter().count()
);
println!("{}", df.pseudocode());
}

Expand All @@ -68,7 +72,7 @@ fn main() -> anyhow::Result<()> {
blk.end_address()
);

if blk.len() != 0 {
if !blk.is_empty() {
let insn = idb.insn_at(blk.start_address()).expect("first instruction");
println!(
"- insn: (ea: {:x}, size: {:x}, operands: {}) ",
Expand Down
4 changes: 4 additions & 0 deletions idalib/src/decompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ impl<'a> CBlock<'a> {
pub fn len(&self) -> usize {
unsafe { idalib_hexrays_cblock_len(self.ptr) }
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
Loading

0 comments on commit efe6584

Please sign in to comment.