-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Fix potential divide by zero #50819
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
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.
Thanks for the fix!
Could you add the test case from #50761 so we could ensure the bug is indeed fixed?
src/librustc_trans/abi.rs
Outdated
} else { | ||
self.rest.total.bytes() / self.rest.unit.size.bytes() | ||
}; | ||
|
||
let rem_bytes = self.rest.total.bytes() % self.rest.unit.size.bytes(); |
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.
If y == 0
, then x % y
will also panic, please move this rem_bytes
calculation inside the conditional as well.
☔ The latest upstream changes (presumably #50615) made this pull request unmergeable. Please resolve the merge conflicts. |
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? |
Sorry for the commit spam after rebasing :( I can remove or squash them when I get a sec again |
@cjkenn Yes, please |
8ae861b
to
7094e7f
Compare
Err hopefully this is better after some more git fiddling -_-... I didn't squash them but the diffs are right. |
@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.
987fb20
to
ecce274
Compare
Ok, I think I've FINALLY got it... Thanks for the git help! |
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.
r=me after a minor nit is fixed.
src/test/ui/issue-50761.rs
Outdated
|
||
// compile-pass | ||
|
||
#![feature(test)] |
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.
This line should be removed?
@bors r+ |
📌 Commit 8d9a87c has been approved by |
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.
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:
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:
rest.unit.size.bytes()
can be 0, and it's ok if it's set to 0 before this function is calledrest.unit.size.bytes()
is 0, that we wantrest_count
to also be 0.