-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue 1057 + improve llvm/state.rs code #1058
Merged
bors
merged 3 commits into
wasmerio:master
from
pventuzelo:ventuzelo/fix-1057-substracte-overflow-peek1
Dec 12, 2019
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,33 +231,41 @@ impl<'ctx> State<'ctx> { | |
|
||
pub fn outermost_frame(&self) -> Result<&ControlFrame<'ctx>, 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, | ||
}) | ||
} | ||
|
||
pub fn frame_at_depth(&self, depth: u32) -> Result<&ControlFrame<'ctx>, BinaryReaderError> { | ||
let index = self.control_stack.len() - 1 - (depth as usize); | ||
self.control_stack.get(index).ok_or(BinaryReaderError { | ||
message: "invalid control stack depth", | ||
offset: -1isize as usize, | ||
}) | ||
let index = self | ||
.control_stack | ||
.len() | ||
.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( | ||
&mut self, | ||
depth: u32, | ||
) -> Result<&mut ControlFrame<'ctx>, BinaryReaderError> { | ||
let index = self.control_stack.len() - 1 - (depth as usize); | ||
self.control_stack.get_mut(index).ok_or(BinaryReaderError { | ||
message: "invalid control stack depth", | ||
offset: -1isize as usize, | ||
}) | ||
let index = self | ||
.control_stack | ||
.len() | ||
.checked_sub(1 + (depth as usize)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too. |
||
.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> { | ||
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, | ||
}) | ||
} | ||
|
@@ -283,7 +291,7 @@ impl<'ctx> State<'ctx> { | |
|
||
pub fn pop1_extra(&mut self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), BinaryReaderError> { | ||
self.stack.pop().ok_or(BinaryReaderError { | ||
message: "invalid value stack", | ||
message: "pop1_extra: invalid value stack", | ||
offset: -1isize as usize, | ||
}) | ||
} | ||
|
@@ -327,13 +335,11 @@ impl<'ctx> State<'ctx> { | |
} | ||
|
||
pub fn peek1_extra(&self) -> Result<(BasicValueEnum<'ctx>, ExtraInfo), BinaryReaderError> { | ||
self.stack | ||
.get(self.stack.len() - 1) | ||
.ok_or(BinaryReaderError { | ||
message: "invalid value stack", | ||
offset: -1isize as usize, | ||
}) | ||
.map(|v| *v) | ||
let index = self.stack.len().checked_sub(1).ok_or(BinaryReaderError { | ||
message: "peek1_extra: invalid value stack", | ||
offset: -1isize as usize, | ||
})?; | ||
Ok(self.stack[index]) | ||
} | ||
|
||
pub fn peekn(&self, n: usize) -> Result<Vec<BasicValueEnum<'ctx>>, BinaryReaderError> { | ||
|
@@ -344,12 +350,12 @@ impl<'ctx> State<'ctx> { | |
&self, | ||
n: usize, | ||
) -> Result<&[(BasicValueEnum<'ctx>, ExtraInfo)], BinaryReaderError> { | ||
let new_len = self.stack.len().checked_sub(n).ok_or(BinaryReaderError { | ||
message: "invalid value stack", | ||
let index = self.stack.len().checked_sub(n).ok_or(BinaryReaderError { | ||
message: "peekn_extra: invalid value stack", | ||
offset: -1isize as usize, | ||
})?; | ||
|
||
Ok(&self.stack[new_len..]) | ||
Ok(&self.stack[index..]) | ||
} | ||
|
||
pub fn popn_save_extra( | ||
|
@@ -362,15 +368,12 @@ impl<'ctx> State<'ctx> { | |
} | ||
|
||
pub fn popn(&mut self, n: usize) -> Result<(), BinaryReaderError> { | ||
if self.stack.len() < n { | ||
return Err(BinaryReaderError { | ||
message: "invalid value stack", | ||
offset: -1isize as usize, | ||
}); | ||
} | ||
let index = self.stack.len().checked_sub(n).ok_or(BinaryReaderError { | ||
message: "popn: invalid value stack", | ||
offset: -1isize as usize, | ||
})?; | ||
|
||
let new_len = self.stack.len() - n; | ||
self.stack.truncate(new_len); | ||
self.stack.truncate(index); | ||
Ok(()) | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not possible because
checked_sub
return anOption
.Do you prefer this way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh! No, I prefer the code as it is, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be possible to use
.and_then
here. I noticed the potential for overflow there too, but I figured it's very unlikely. I think it should be fine as-is!For future readers who may end up here who didn't understand what I meant:
and_then
acts like Monadic bind (>>=
) in Haskell.