@@ -445,6 +445,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
445445 ' tcx : ' a ,
446446 ' mir : ' a ,
447447 {
448+ #[ cfg( target_os = "unix" ) ]
449+ fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
450+ Ok ( std:: os:: unix:: ffi:: OsStringExt :: from_bytes ( bytes) )
451+ }
452+ #[ cfg( not( target_os = "unix" ) ) ]
453+ fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
454+ let s = std:: str:: from_utf8 ( bytes)
455+ . map_err ( |_| err_unsup_format ! ( "{:?} is not a valid utf-8 string" , bytes) ) ?;
456+ Ok ( & OsStr :: new ( s) )
457+ }
458+
448459 let this = self . eval_context_ref ( ) ;
449460 let bytes = this. memory . read_c_str ( scalar) ?;
450461 bytes_to_os_str ( bytes)
@@ -460,6 +471,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
460471 scalar : Scalar < Tag > ,
461472 size : u64 ,
462473 ) -> InterpResult < ' tcx , bool > {
474+ #[ cfg( target_os = "unix" ) ]
475+ fn os_str_to_bytes < ' tcx , ' a > ( os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
476+ std:: os:: unix:: ffi:: OsStringExt :: into_bytes ( os_str)
477+ }
478+ #[ cfg( not( target_os = "unix" ) ) ]
479+ fn os_str_to_bytes < ' tcx , ' a > ( os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
480+ // On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
481+ // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
482+ // valid.
483+ os_str
484+ . to_str ( )
485+ . map ( |s| s. as_bytes ( ) )
486+ . ok_or_else ( || err_unsup_format ! ( "{:?} is not a valid utf-8 string" , os_str) . into ( ) )
487+ }
488+
463489 let bytes = os_str_to_bytes ( os_str) ?;
464490 // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
465491 // terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
@@ -473,34 +499,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
473499 }
474500}
475501
476- #[ cfg( target_os = "unix" ) ]
477- fn os_str_to_bytes < ' tcx , ' a > ( os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
478- std:: os:: unix:: ffi:: OsStringExt :: into_bytes ( os_str)
479- }
480-
481- #[ cfg( target_os = "unix" ) ]
482- fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
483- Ok ( std:: os:: unix:: ffi:: OsStringExt :: from_bytes ( bytes) )
484- }
485-
486- // On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
487- // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
488- // valid.
489- #[ cfg( not( target_os = "unix" ) ) ]
490- fn os_str_to_bytes < ' tcx , ' a > ( os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
491- os_str
492- . to_str ( )
493- . map ( |s| s. as_bytes ( ) )
494- . ok_or_else ( || err_unsup_format ! ( "{:?} is not a valid utf-8 string" , os_str) . into ( ) )
495- }
496-
497- #[ cfg( not( target_os = "unix" ) ) ]
498- fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
499- let s = std:: str:: from_utf8 ( bytes)
500- . map_err ( |_| err_unsup_format ! ( "{:?} is not a valid utf-8 string" , bytes) ) ?;
501- Ok ( & OsStr :: new ( s) )
502- }
503-
504502pub fn immty_from_int_checked < ' tcx > (
505503 int : impl Into < i128 > ,
506504 layout : TyLayout < ' tcx > ,
0 commit comments