Skip to content

Commit abf02fd

Browse files
committed
Throw error instead of panicking for unfittable bits
1 parent d76ab94 commit abf02fd

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/helpers.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
44
use rustc::mir;
55
use rustc::ty::{
66
self,
7-
layout::{self, Align, Size, LayoutOf},
7+
layout::{self, Align, LayoutOf, Size},
88
};
99

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

318-
for (value, size) in bits.iter().zip(sizes) {
318+
for (&value, size) in bits.iter().zip(sizes) {
319+
// If `value` does not fit in `size` bits, we error instead of letting
320+
// `Scalar::from_int` panic.
321+
let truncated = truncate(value as u128, size);
322+
if sign_extend(truncated, size) as i128 != value {
323+
throw_unsup_format!(
324+
"Signed value {:#x} does not fit in {} bits",
325+
value,
326+
size.bits()
327+
)
328+
}
329+
319330
allocation.write_scalar(
320331
tcx,
321332
ptr.offset(offset, tcx)?,
322-
Scalar::from_int(*value, size).into(),
333+
Scalar::from_int(value, size).into(),
323334
size,
324335
)?;
325336
offset += size;

0 commit comments

Comments
 (0)