@@ -82,8 +82,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8282 let align = this. min_align ( size, kind) ;
8383 let ptr = this. allocate_ptr ( Size :: from_bytes ( size) , align, kind. into ( ) ) ?;
8484 if zero_init {
85- // We just allocated this, the access is definitely in-bounds.
86- this. write_bytes_ptr ( ptr. into ( ) , iter:: repeat ( 0u8 ) . take ( size as usize ) ) . unwrap ( ) ;
85+ // We just allocated this, the access is definitely in-bounds and fits into our address space.
86+ this. write_bytes_ptr (
87+ ptr. into ( ) ,
88+ iter:: repeat ( 0u8 ) . take ( usize:: try_from ( size) . unwrap ( ) ) ,
89+ )
90+ . unwrap ( ) ;
8791 }
8892 Ok ( ptr. into ( ) )
8993 }
@@ -526,8 +530,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
526530 "memrchr" => {
527531 let [ ptr, val, num] = this. check_shim ( abi, Abi :: C { unwind : false } , link_name, args) ?;
528532 let ptr = this. read_pointer ( ptr) ?;
529- let val = this. read_scalar ( val) ?. to_i32 ( ) ? as u8 ;
533+ let val = this. read_scalar ( val) ?. to_i32 ( ) ?;
530534 let num = this. read_scalar ( num) ?. to_machine_usize ( this) ?;
535+ // The docs say val is "interpreted as unsigned char".
536+ #[ allow( clippy:: cast_sign_loss, clippy:: cast_possible_truncation) ]
537+ let val = val as u8 ;
538+
531539 if let Some ( idx) = this
532540 . read_bytes_ptr ( ptr, Size :: from_bytes ( num) ) ?
533541 . iter ( )
@@ -543,8 +551,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
543551 "memchr" => {
544552 let [ ptr, val, num] = this. check_shim ( abi, Abi :: C { unwind : false } , link_name, args) ?;
545553 let ptr = this. read_pointer ( ptr) ?;
546- let val = this. read_scalar ( val) ?. to_i32 ( ) ? as u8 ;
554+ let val = this. read_scalar ( val) ?. to_i32 ( ) ?;
547555 let num = this. read_scalar ( num) ?. to_machine_usize ( this) ?;
556+ // The docs say val is "interpreted as unsigned char".
557+ #[ allow( clippy:: cast_sign_loss, clippy:: cast_possible_truncation) ]
558+ let val = val as u8 ;
559+
548560 let idx = this
549561 . read_bytes_ptr ( ptr, Size :: from_bytes ( num) ) ?
550562 . iter ( )
0 commit comments