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 potential divide by zero #50819

Merged
merged 2 commits into from
May 20, 2018
Merged

Fix potential divide by zero #50819

merged 2 commits into from
May 20, 2018

Conversation

cjkenn
Copy link
Contributor

@cjkenn cjkenn commented May 17, 2018

This should fix #50761

I had trouble reproducing with the provided code, but looking at the stack trace would indicate that this code is the likely cause. I made a number of assumptions here, because I don't have enough context on how the register size is set:

  1. I assumed rest.unit.size.bytes() can be 0, and it's ok if it's set to 0 before this function is called
  2. I assumed that if rest.unit.size.bytes() is 0, that we want rest_count to also be 0.

@rust-highfive
Copy link
Collaborator

r? @michaelwoerister

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 17, 2018
Copy link
Member

@kennytm kennytm left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

Could you add the test case from #50761 so we could ensure the bug is indeed fixed?

} else {
self.rest.total.bytes() / self.rest.unit.size.bytes()
};

let rem_bytes = self.rest.total.bytes() % self.rest.unit.size.bytes();
Copy link
Member

Choose a reason for hiding this comment

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

If y == 0, then x % y will also panic, please move this rem_bytes calculation inside the conditional as well.

@kennytm kennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 17, 2018
@bors
Copy link
Contributor

bors commented May 17, 2018

☔ The latest upstream changes (presumably #50615) made this pull request unmergeable. Please resolve the merge conflicts.

@cjkenn
Copy link
Contributor Author

cjkenn commented May 18, 2018

Should the test file be added to codegen tests? I've added a ui test because we just want to ensure that the code from the issue passes compilation right?

@cjkenn
Copy link
Contributor Author

cjkenn commented May 18, 2018

Sorry for the commit spam after rebasing :( I can remove or squash them when I get a sec again

@michaelwoerister
Copy link
Member

@cjkenn Yes, please :)

@cjkenn
Copy link
Contributor Author

cjkenn commented May 18, 2018

Err hopefully this is better after some more git fiddling -_-... I didn't squash them but the diffs are right.

@kennytm kennytm dismissed their stale review May 18, 2018 16:02

(Changes applied)

@kennytm
Copy link
Member

kennytm commented May 18, 2018

@cjkenn Could you do a rebase instead of merge?

$ git checkout master
$ git pull origin master --ff-only    # synchronize master branch with this repository
$ git checkout div-by-zero
$ git rebase master      # rebase on top of the latest master
$ git rebase -i master   # do more cleanup e.g. squash everything down to a single commit
$ git push cjkenn div-by-zero --force-with-lease

remove semicolon -_-

Add rem_bytes to conditional to avoid error when performing mod by 0

Add test file to confirm compilation passes.

Ensure we don't divide or mod by zero in llvm_type. Include test file from issue.
@cjkenn
Copy link
Contributor Author

cjkenn commented May 19, 2018

Ok, I think I've FINALLY got it... Thanks for the git help!

Copy link
Member

@kennytm kennytm left a comment

Choose a reason for hiding this comment

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

r=me after a minor nit is fixed.


// compile-pass

#![feature(test)]
Copy link
Member

Choose a reason for hiding this comment

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

This line should be removed?

@kennytm
Copy link
Member

kennytm commented May 19, 2018

@bors r+

@bors
Copy link
Contributor

bors commented May 19, 2018

📌 Commit 8d9a87c has been approved by kennytm

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 19, 2018
kennytm added a commit to kennytm/rust that referenced this pull request May 19, 2018
Fix potential divide by zero

This should fix rust-lang#50761

I had trouble reproducing with the provided code, but looking at the stack trace would indicate that this code is the likely cause. I made a number of assumptions here, because I don't have enough context on how the register size is set:

1. I assumed `rest.unit.size.bytes()` can be 0, and it's ok if it's set to 0 before this function is called
2. I assumed that if `rest.unit.size.bytes()` is 0, that we want `rest_count` to also be 0.
bors added a commit that referenced this pull request May 19, 2018
Rollup of 8 pull requests

Successful merges:

 - #50531 (Cleanup uses of TypeIdHasher and replace them with StableHasher)
 - #50819 (Fix potential divide by zero)
 - #50827 (Update LLVM to 56c931901cfb85cd6f7ed44c7d7520a8de1edf97)
 - #50829 (CheckLoopVisitor: also visit break expressions)
 - #50854 (in which the unused shorthand field pattern debacle/saga continues)
 - #50858 (Reorder description for snippets in rustdoc documentation)
 - #50883 (Fix warning when building stage0 libcore)
 - #50889 (Update clippy)

Failed merges:
@bors bors merged commit 8d9a87c into rust-lang:master May 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rustc panic with divide by zero
5 participants