diff --git a/src/impls.rs b/src/impls.rs index 7c7f7e0..d04defb 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -15,6 +15,10 @@ pub trait StringToCString { fn to_c_string(self) -> *mut c_char; } +pub trait DropCString { + fn drop_c_string(self); +} + pub trait ErrorToInt { fn to_int(&self) -> i64; } @@ -83,6 +87,16 @@ impl From<&mut Vec<&str>> for CharVec { } } +impl From for Vec<*mut c_char> { + fn from(value: CharVec) -> Self { + if value.vec.is_null() || value.length == 0 { + return Vec::new(); + }; + + unsafe { Vec::from_raw_parts(value.vec, value.length, value.capacity) } + } +} + impl StringToCString for T where T: Display, @@ -101,6 +115,26 @@ where } } +impl DropCString for *mut c_char { + fn drop_c_string(self) { + unsafe { + if !self.is_null() { + let _ = CString::from_raw(self); + } + } + } +} + +impl DropCString for CharVec { + fn drop_c_string(self) { + let vector: Vec<*mut c_char> = self.into(); + + for item in vector { + item.drop_c_string(); + } + } +} + impl ErrorToInt for goblin::error::Error { fn to_int(&self) -> i64 { match self { diff --git a/src/lib.rs b/src/lib.rs index 2bf1f5c..ec784c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,7 @@ //! //! [^3]: That doesn't mean I am liable for any damages done by this project and files you parsed. Take your own risk! //! +use crate::impls::{DropCString, ErrorToInt, StringToCString}; use archive::parse_archive; use coff::parse_coff; use debug::merge_members; @@ -36,11 +37,8 @@ use goblin::Object; use mach::parse_mach; use owo_colors::OwoColorize; use pe::parse_pe; -use std::ffi::{c_char, CStr, CString}; -use structs::{ - CharVec, Debugging, ParsingError, ULDDObj, ULDDObjResult, ULDDObjResultVec, -}; -use crate::impls::{ErrorToInt, StringToCString}; +use std::ffi::{c_char, CStr}; +use structs::{CharVec, Debugging, ParsingError, ULDDObj, ULDDObjResult, ULDDObjResultVec}; #[doc(hidden)] pub mod archive; @@ -218,12 +216,6 @@ pub unsafe extern "C" fn read_obj( ULDDObjResultVec::from(objects) } -unsafe fn drop_c_string(ptr: *mut c_char) { - if !ptr.is_null() { - let _ = CString::from_raw(ptr); - } -} - /// /// # Safety /// @@ -250,31 +242,18 @@ pub unsafe extern "C" fn free_obj(obj: ULDDObjResultVec, debugging: bool) -> u8 Debugging::Info(format!("{}. object is being deallocated", index + 1)).print(debugging); let o = object.obj; - drop_c_string(object.error.explanation); - drop_c_string(o.file_name); - drop_c_string(o.executable_format); - drop_c_string(o.os_type); - drop_c_string(o.file_type); - drop_c_string(o.cpu_type); - drop_c_string(o.cpu_subtype); - drop_c_string(o.interpreter); - if !o.member_name.vec.is_null() { - let member_names = Vec::from_raw_parts( - o.member_name.vec, - o.member_name.length, - o.member_name.capacity, - ); - for name in member_names { - drop_c_string(name) - } - }; - if !o.libraries.vec.is_null() { - let libraries = - Vec::from_raw_parts(o.libraries.vec, o.libraries.length, o.libraries.capacity); - for library in libraries { - drop_c_string(library) - } - }; + + object.error.explanation.drop_c_string(); + o.file_name.drop_c_string(); + o.executable_format.drop_c_string(); + o.os_type.drop_c_string(); + o.file_type.drop_c_string(); + o.cpu_type.drop_c_string(); + o.cpu_subtype.drop_c_string(); + o.interpreter.drop_c_string(); + o.member_name.drop_c_string(); + o.libraries.drop_c_string(); + Debugging::Affirmative(format!("{}. object is deallocated", index + 1)).print(debugging); }