Skip to content

Commit e1f031e

Browse files
committed
Rollup merge of rust-lang#50819 - cjkenn:cjkenn/div-by-zero, r=kennytm
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.
2 parents 9f34c7f + 8d9a87c commit e1f031e

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/librustc_codegen_llvm/abi.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ impl LlvmType for Reg {
127127
impl LlvmType for CastTarget {
128128
fn llvm_type(&self, cx: &CodegenCx) -> Type {
129129
let rest_ll_unit = self.rest.unit.llvm_type(cx);
130-
let rest_count = self.rest.total.bytes() / self.rest.unit.size.bytes();
131-
let rem_bytes = self.rest.total.bytes() % self.rest.unit.size.bytes();
130+
let (rest_count, rem_bytes) = if self.rest.unit.size.bytes() == 0 {
131+
(0, 0)
132+
} else {
133+
(self.rest.total.bytes() / self.rest.unit.size.bytes(),
134+
self.rest.total.bytes() % self.rest.unit.size.bytes())
135+
};
132136

133137
if self.prefix.iter().all(|x| x.is_none()) {
134138
// Simplify to a single unit when there is no prefix and size <= unit size

src/test/ui/issue-50761.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Confirm that we don't accidently divide or mod by zero in llvm_type
12+
13+
// compile-pass
14+
15+
mod a {
16+
pub trait A {}
17+
}
18+
19+
mod b {
20+
pub struct Builder {}
21+
22+
pub fn new() -> Builder {
23+
Builder {}
24+
}
25+
26+
impl Builder {
27+
pub fn with_a(&mut self, _a: fn() -> ::a::A) {}
28+
}
29+
}
30+
31+
pub use self::b::new;
32+
33+
fn main() {}

0 commit comments

Comments
 (0)