Skip to content
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

Conversation

pventuzelo
Copy link
Contributor

@pventuzelo pventuzelo commented Dec 11, 2019

Description

This pull request is doing:

Review

  • Add a short description of the the change to the CHANGELOG.md file

let index = self
.control_stack
.len()
.checked_sub(1 + (depth as usize))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

let index = self
.control_stack
.len()
.checked_sub(1 + (depth as usize))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.checked_sub(1)
.checked_sub(depth as usize)

?

Copy link
Contributor Author

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 an Option.

Do you prefer this way?

        let tmp_index = self
            .control_stack
            .len()
            .checked_sub(1).ok_or(BinaryReaderError {
                message: "frame_at_depth: invalid control stack depth",
                offset: -1isize as usize,
            })?;
        let index = tmp_index.checked_sub(depth as usize)
            .ok_or(BinaryReaderError {
                message: "frame_at_depth: invalid control stack depth",
                offset: -1isize as usize,
            })?;
        Ok(&self.control_stack[index])

Copy link
Contributor

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.

Copy link
Contributor

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:

self
            .control_stack
            .len()
            .checked_sub(1)
            .and_then(|n| n.checked_sub(depth as usize))
            .ok_or(BinaryReaderError {
                message: "frame_at_depth: invalid control stack depth",
                offset: -1isize as usize,
            })?;

and_then acts like Monadic bind (>>=) in Haskell.

let index = self
.control_stack
.len()
.checked_sub(1 + (depth as usize))
Copy link
Contributor

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.

@nlewycky
Copy link
Contributor

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 12, 2019

Merge conflict

@syrusakbary
Copy link
Member

bors r+

bors bot added a commit that referenced this pull request Dec 12, 2019
1058: Fix issue 1057 + improve llvm/state.rs code r=syrusakbary a=pventuzelo

# Description

This pull request is doing:
- Fix issue #1057 (subtraction overflow panic in State::peek1_extra) by using `checked_sub`
- replace other part of `state.rs` subject to potential substration overflow with `checked_sub`
- add more detail on the Errors messages in `state.rs`
- rename some variable for better understanding of the code.

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Patrick Ventuzelo <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants