insert more `abort()` calls where division by zero may occur

This commit is contained in:
Jorge Aparicio 2016-10-05 20:45:40 -05:00
parent a2ffc799d0
commit a21fb956f9
2 changed files with 22 additions and 0 deletions

View File

@ -1,3 +1,5 @@
use core::intrinsics;
use int::Int; use int::Int;
macro_rules! div { macro_rules! div {
@ -10,6 +12,12 @@ macro_rules! div {
let a = (a ^ s_a) - s_a; let a = (a ^ s_a) - s_a;
let b = (b ^ s_b) - s_b; let b = (b ^ s_b) - s_b;
let s = s_a ^ s_b; let s = s_a ^ s_b;
if b == 0 {
unsafe {
intrinsics::abort()
}
}
let r = (a as $uty) / (b as $uty); let r = (a as $uty) / (b as $uty);
(r as $ty ^ s) - s (r as $ty ^ s) - s
} }
@ -25,6 +33,12 @@ macro_rules! mod_ {
let b = (b ^ s) - s; let b = (b ^ s) - s;
let s = a >> (<$ty>::bits() - 1); let s = a >> (<$ty>::bits() - 1);
let a = (a ^ s) - s; let a = (a ^ s) - s;
if b == 0 {
unsafe {
intrinsics::abort()
}
}
let r = (a as $uty) % (b as $uty); let r = (a as $uty) % (b as $uty);
(r as $ty ^ s) - s (r as $ty ^ s) - s
} }

View File

@ -109,6 +109,14 @@ pub extern "C" fn __udivmoddi4(n: u64, d: u64, rem: Option<&mut u64>) -> u64 {
// 0 X // 0 X
// --- // ---
// 0 X // 0 X
// NOTE This should be unreachable in safe Rust because the program will panic before
// this intrinsic is called
if d.low() == 0 {
unsafe {
intrinsics::abort()
}
}
if let Some(rem) = rem { if let Some(rem) = rem {
*rem = u64::from(n.low() % d.low()); *rem = u64::from(n.low() % d.low());
} }