rename some variable, add more precise error message, use checked_sub everywhere needed in state.rs

This commit is contained in:
Patrick Ventuzelo 2019-12-11 15:25:21 +01:00
parent ca03402207
commit 6189c9ae54

View File

@ -231,33 +231,41 @@ impl<'ctx> State<'ctx> {
pub fn outermost_frame(&self) -> Result<&ControlFrame<'ctx>, BinaryReaderError> { pub fn outermost_frame(&self) -> Result<&ControlFrame<'ctx>, BinaryReaderError> {
self.control_stack.get(0).ok_or(BinaryReaderError { self.control_stack.get(0).ok_or(BinaryReaderError {
message: "invalid control stack depth", message: "outermost_frame: invalid control stack depth",
offset: -1isize as usize, offset: -1isize as usize,
}) })
} }
pub fn frame_at_depth(&self, depth: u32) -> Result<&ControlFrame<'ctx>, BinaryReaderError> { pub fn frame_at_depth(&self, depth: u32) -> Result<&ControlFrame<'ctx>, BinaryReaderError> {
let index = self.control_stack.len() - 1 - (depth as usize); let index = self
self.control_stack.get(index).ok_or(BinaryReaderError { .control_stack
message: "invalid control stack depth", .len()
offset: -1isize as usize, .checked_sub(1 + (depth as usize))
}) .ok_or(BinaryReaderError {
message: "frame_at_depth: invalid control stack depth",
offset: -1isize as usize,
})?;
Ok(&self.control_stack[index])
} }
pub fn frame_at_depth_mut( pub fn frame_at_depth_mut(
&mut self, &mut self,
depth: u32, depth: u32,
) -> Result<&mut ControlFrame<'ctx>, BinaryReaderError> { ) -> Result<&mut ControlFrame<'ctx>, BinaryReaderError> {
let index = self.control_stack.len() - 1 - (depth as usize); let index = self
self.control_stack.get_mut(index).ok_or(BinaryReaderError { .control_stack
message: "invalid control stack depth", .len()
offset: -1isize as usize, .checked_sub(1 + (depth as usize))
}) .ok_or(BinaryReaderError {
message: "frame_at_depth_mut: invalid control stack depth",
offset: -1isize as usize,
})?;
Ok(&mut self.control_stack[index])
} }
pub fn pop_frame(&mut self) -> Result<ControlFrame<'ctx>, BinaryReaderError> { pub fn pop_frame(&mut self) -> Result<ControlFrame<'ctx>, BinaryReaderError> {
self.control_stack.pop().ok_or(BinaryReaderError { self.control_stack.pop().ok_or(BinaryReaderError {
message: "cannot pop from control stack", message: "pop_frame: cannot pop from control stack",
offset: -1isize as usize, offset: -1isize as usize,
}) })
} }
@ -283,7 +291,7 @@ impl<'ctx> State<'ctx> {
pub fn pop1_extra(&mut self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), BinaryReaderError> { pub fn pop1_extra(&mut self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), BinaryReaderError> {
self.stack.pop().ok_or(BinaryReaderError { self.stack.pop().ok_or(BinaryReaderError {
message: "invalid value stack", message: "pop1_extra: invalid value stack",
offset: -1isize as usize, offset: -1isize as usize,
}) })
} }
@ -327,13 +335,11 @@ impl<'ctx> State<'ctx> {
} }
pub fn peek1_extra(&self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), BinaryReaderError> { pub fn peek1_extra(&self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), BinaryReaderError> {
self.stack let index = self.stack.len().checked_sub(1).ok_or(BinaryReaderError {
.get(self.stack.len() - 1) message: "peek1_extra: invalid value stack",
.ok_or(BinaryReaderError { offset: -1isize as usize,
message: "invalid value stack", })?;
offset: -1isize as usize, Ok(self.stack[index])
})
.map(|v| *v)
} }
pub fn peekn(&self, n: usize) -> Result<Vec<BasicValueEnum<'ctx>>, BinaryReaderError> { pub fn peekn(&self, n: usize) -> Result<Vec<BasicValueEnum<'ctx>>, BinaryReaderError> {
@ -344,12 +350,12 @@ impl<'ctx> State<'ctx> {
&self, &self,
n: usize, n: usize,
) -> Result<&[(BasicValueEnum<'ctx>, ExtraInfo)], BinaryReaderError> { ) -> Result<&[(BasicValueEnum<'ctx>, ExtraInfo)], BinaryReaderError> {
let new_len = self.stack.len().checked_sub(n).ok_or(BinaryReaderError { let index = self.stack.len().checked_sub(n).ok_or(BinaryReaderError {
message: "invalid value stack", message: "peekn_extra: invalid value stack",
offset: -1isize as usize, offset: -1isize as usize,
})?; })?;
Ok(&self.stack[new_len..]) Ok(&self.stack[index..])
} }
pub fn popn_save_extra( pub fn popn_save_extra(
@ -362,15 +368,12 @@ impl<'ctx> State<'ctx> {
} }
pub fn popn(&mut self, n: usize) -> Result<(), BinaryReaderError> { pub fn popn(&mut self, n: usize) -> Result<(), BinaryReaderError> {
if self.stack.len() < n { let index = self.stack.len().checked_sub(n).ok_or(BinaryReaderError {
return Err(BinaryReaderError { message: "popn: invalid value stack",
message: "invalid value stack", offset: -1isize as usize,
offset: -1isize as usize, })?;
});
}
let new_len = self.stack.len() - n; self.stack.truncate(index);
self.stack.truncate(new_len);
Ok(()) Ok(())
} }