Skip to content

Commit

Permalink
Throw error instead of panicking for unfittable bits
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Oct 9, 2019
1 parent e00db1b commit 07abfb3
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc::mir;
use rustc::ty::{
self,
layout::{self, Align, Size, LayoutOf},
layout::{self, Align, LayoutOf, Size},
};

use rand::RngCore;
Expand Down Expand Up @@ -316,11 +316,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let allocation = this.memory_mut().get_mut(ptr.alloc_id)?;
let mut offset = Size::from_bytes(0);

for (value, size) in bits.iter().zip(sizes) {
for (&value, size) in bits.iter().zip(sizes) {
// If `value` does not fit in `size` bits, we error instead of letting
// `Scalar::from_int` panic.
let truncated = truncate(value as u128, size);
if sign_extend(truncated, size) as i128 != value {
throw_unsup_format!(
"Signed value {:#x} does not fit in {} bits",
value,
size.bits()
)
}

allocation.write_scalar(
tcx,
ptr.offset(offset, tcx)?,
Scalar::from_int(*value, size).into(),
Scalar::from_int(value, size).into(),
size,
)?;
offset += size;
Expand Down

0 comments on commit 07abfb3

Please sign in to comment.