diff --git a/src/error.rs b/src/error.rs index 9350f4d..ceacc28 100644 --- a/src/error.rs +++ b/src/error.rs @@ -15,6 +15,9 @@ pub enum Error { /// there was a failure in building the Process or Module struct #[error("failed to get process/module")] ProcessError(String), + /// there was a failure in resolving a pointer chain + #[error("failed to resolve pointer chain")] + ResolvePointerChainError(String), /// there was a failure in conversion between integers #[error("failed to convert integer")] ConvertIntegerError(#[from] std::num::TryFromIntError), diff --git a/src/process.rs b/src/process.rs index 1370cc3..5586cf9 100644 --- a/src/process.rs +++ b/src/process.rs @@ -101,13 +101,15 @@ impl Process { /// follow a pointer chain to the end and return an address pub fn resolve_pointer_chain(&self, chain: &[usize]) -> Result { - let mut chain = chain.to_vec(); - let mut address = chain.remove(0); - while chain.len() > 1 { - address += chain.remove(0); + if chain.is_empty() { + return Err(Error::ResolvePointerChainError("chain was empty".into())); + } + let mut address = chain[0]; + for &offset in &chain[1..(chain.len() - 1)] { + address += offset; address = self.read_mem(address)?; } - Ok(address + chain.remove(0)) + Ok(address + chain.last().expect("chain should have a last element")) } /// read a given `address` of process's memory