Skip to content

Commit b10fc7a

Browse files
committed
make sure miri never switches over an invalid char value
1 parent 7a9272c commit b10fc7a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/interpreter/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,17 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
406406

407407
SwitchInt { ref discr, ref values, ref targets, .. } => {
408408
let discr_ptr = self.eval_lvalue(discr)?.to_ptr();
409+
let discr_ty = self.lvalue_ty(discr);
409410
let discr_size = self
410-
.type_layout(self.lvalue_ty(discr))
411+
.type_layout(discr_ty)
411412
.size(&self.tcx.data_layout)
412413
.bytes() as usize;
413414
let discr_val = self.memory.read_uint(discr_ptr, discr_size)?;
415+
if let ty::TyChar = discr_ty.sty {
416+
if ::std::char::from_u32(discr_val as u32).is_none() {
417+
return Err(EvalError::InvalidChar(discr_val as u32));
418+
}
419+
}
414420

415421
// Branch to the `otherwise` case by default, if no match is found.
416422
let mut target_block = targets[targets.len() - 1];

0 commit comments

Comments
 (0)