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

Handle overflow errors in Bytes -> Pages conversion #1914

Merged
merged 1 commit into from
Dec 10, 2020

Conversation

webmaster128
Copy link
Contributor

@webmaster128 webmaster128 commented Dec 10, 2020

Description

Before the Bytes to Pages conversion silently returned wrong results when the number of pages was too large.

Review

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

Copy link
Contributor

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

Thank you very much for the PR! Apart from the little comment, everything looks good to me!

impl From<Bytes> for Pages {
fn from(bytes: Bytes) -> Self {
Self((bytes.0 / WASM_PAGE_SIZE) as u32)
const PAGES_EXCEED_UINT32: &'static str = "Number of pages exceeds uint32 range";
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need a constant here. The constant is used only once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can inline it, no problem. Do you prefer that?

Comment on lines 119 to 120
.try_into()
.or(Err(PAGES_EXCEED_UINT32))?;
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you can write:

Suggested change
.try_into()
.or(Err(PAGES_EXCEED_UINT32))?;
.try_into()
.map_err(PAGES_EXCEED_UINT32)?;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

map_err takes a callback, which is what I wanted to avoid. It would have to be

.map_err(|_original_error| PAGES_EXCEED_UINT32)?;

However, we can use this as well. Which way do you like more?

@webmaster128
Copy link
Contributor Author

Thanks for the positive feedback @Hywan. After looking at the error again I thought we better do this properly and use an error type instead of a generic string. This shows the user's type system that there there is only one possible error reason that in many use cases can be handled by defauling to u32::MAX.

@syrusakbary
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 10, 2020

@bors bors bot merged commit 51bd835 into wasmerio:master Dec 10, 2020
@webmaster128 webmaster128 deleted the bytes-to-pages branch December 10, 2020 20:05
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.

3 participants