Skip to content

Commit 1f9fa53

Browse files
committed
Sanity check the bits argument to the from_bits function
1 parent f1ea9ef commit 1f9fa53

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/librustc/ty/sty.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,10 +1824,13 @@ impl<'tcx> Const<'tcx> {
18241824
ty: ParamEnvAnd<'tcx, Ty<'tcx>>,
18251825
) -> &'tcx Self {
18261826
let ty = tcx.global_tcx().lift(&ty).unwrap();
1827-
let defined = tcx.global_tcx().layout_of(ty).unwrap_or_else(|e| {
1827+
let size = tcx.global_tcx().layout_of(ty).unwrap_or_else(|e| {
18281828
panic!("could not compute layout for {:?}: {:?}", ty, e)
1829-
}).size.bits() as u8;
1830-
Self::from_scalar(tcx, Scalar::Bits { bits, defined }, ty.value)
1829+
}).size;
1830+
let amt = 128 - size.bits();
1831+
let truncated = (bits << amt) >> amt;
1832+
assert_eq!(truncated, bits, "from_bits called with untruncated value");
1833+
Self::from_scalar(tcx, Scalar::Bits { bits, defined: size.bits() as u8 }, ty.value)
18311834
}
18321835

18331836
#[inline]

0 commit comments

Comments
 (0)