Skip to content

Commit e20c91a

Browse files
author
Jorge Aparicio
committed
insert more abort() calls where division by zero may occur
1 parent 41bb10a commit e20c91a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/int/sdiv.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::intrinsics;
2+
13
use int::Int;
24

35
macro_rules! div {
@@ -10,6 +12,12 @@ macro_rules! div {
1012
let a = (a ^ s_a) - s_a;
1113
let b = (b ^ s_b) - s_b;
1214
let s = s_a ^ s_b;
15+
16+
if b == 0 {
17+
unsafe {
18+
intrinsics::abort()
19+
}
20+
}
1321
let r = (a as $uty) / (b as $uty);
1422
(r as $ty ^ s) - s
1523
}
@@ -25,6 +33,12 @@ macro_rules! mod_ {
2533
let b = (b ^ s) - s;
2634
let s = a >> (<$ty>::bits() - 1);
2735
let a = (a ^ s) - s;
36+
37+
if b == 0 {
38+
unsafe {
39+
intrinsics::abort()
40+
}
41+
}
2842
let r = (a as $uty) % (b as $uty);
2943
(r as $ty ^ s) - s
3044
}

src/int/udiv.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ pub extern "C" fn __udivmoddi4(n: u64, d: u64, rem: Option<&mut u64>) -> u64 {
109109
// 0 X
110110
// ---
111111
// 0 X
112+
// NOTE This should be unreachable in safe Rust because the program will panic before
113+
// this intrinsic is called
114+
if d.low() == 0 {
115+
unsafe {
116+
intrinsics::abort()
117+
}
118+
}
119+
112120
if let Some(rem) = rem {
113121
*rem = u64::from(n.low() % d.low());
114122
}

0 commit comments

Comments
 (0)