Skip to content

Commit a553fa7

Browse files
committed
Fix integer overflow
1 parent 7695bd0 commit a553fa7

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
475475
let min_max_ty = |sty| {
476476
let size = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(sty))
477477
.unwrap().size.bits() as i128;
478-
let min = -(1 << (size - 1));
479-
let max = (1 << (size - 1)) - 1;
478+
let min = (1i128 << (size - 1)).wrapping_neg();
479+
let max = (1i128 << (size - 1)).wrapping_sub(1);
480480
(min as u128, max as u128, sty)
481481
};
482482
let (min, max, ty) = match int_ty {
@@ -496,8 +496,9 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
496496
use syntax::ast::UintTy::*;
497497
let min_max_ty = |sty| {
498498
let size = cx.tcx.layout_of(ty::ParamEnv::reveal_all().and(sty))
499-
.unwrap().size.bits() as i128;
500-
let max = (1 << size) - 1;
499+
.unwrap().size.bits() as u32;
500+
let shift = 1u128.overflowing_shl(size);
501+
let max = shift.0.wrapping_sub(1 + (shift.1 as u128));
501502
(0u128, max as u128, sty)
502503
};
503504
let (min, max, ty) = match uint_ty {

0 commit comments

Comments
 (0)