@@ -62,27 +62,28 @@ fn convert_path_separator<'a>(
62
62
63
63
impl < ' mir , ' tcx : ' mir > EvalContextExt < ' mir , ' tcx > for crate :: MiriEvalContext < ' mir , ' tcx > { }
64
64
pub trait EvalContextExt < ' mir , ' tcx : ' mir > : crate :: MiriEvalContextExt < ' mir , ' tcx > {
65
+
66
+ #[ cfg( unix) ]
67
+ fn bytes_to_os_str < ' a > ( & self , bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
68
+ Ok ( OsStr :: from_bytes ( bytes) )
69
+ }
70
+ #[ cfg( not( unix) ) ]
71
+ fn bytes_to_os_str < ' a > ( & self , bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
72
+ let s = std:: str:: from_utf8 ( bytes)
73
+ . map_err ( |_| err_unsup_format ! ( "{:?} is not a valid utf-8 string" , bytes) ) ?;
74
+ Ok ( OsStr :: new ( s) )
75
+ }
76
+
65
77
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
66
78
/// the Unix APIs usually handle.
67
79
fn read_os_str_from_c_str < ' a > ( & ' a self , scalar : Scalar < Tag > ) -> InterpResult < ' tcx , & ' a OsStr >
68
80
where
69
81
' tcx : ' a ,
70
82
' mir : ' a ,
71
83
{
72
- #[ cfg( unix) ]
73
- fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
74
- Ok ( OsStr :: from_bytes ( bytes) )
75
- }
76
- #[ cfg( not( unix) ) ]
77
- fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
78
- let s = std:: str:: from_utf8 ( bytes)
79
- . map_err ( |_| err_unsup_format ! ( "{:?} is not a valid utf-8 string" , bytes) ) ?;
80
- Ok ( OsStr :: new ( s) )
81
- }
82
-
83
84
let this = self . eval_context_ref ( ) ;
84
85
let bytes = this. memory . read_c_str ( scalar) ?;
85
- bytes_to_os_str ( bytes)
86
+ self . bytes_to_os_str ( bytes)
86
87
}
87
88
88
89
/// Helper function to read an OsString from a 0x0000-terminated sequence of u16,
@@ -107,6 +108,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
107
108
u16vec_to_osstring ( u16_vec)
108
109
}
109
110
111
+ #[ cfg( unix) ]
112
+ fn os_str_to_bytes < ' a > ( & self , os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
113
+ Ok ( os_str. as_bytes ( ) )
114
+ }
115
+
116
+ #[ cfg( not( unix) ) ]
117
+ fn os_str_to_bytes < ' a > ( & self , os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
118
+ // On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
119
+ // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
120
+ // valid.
121
+ os_str
122
+ . to_str ( )
123
+ . map ( |s| s. as_bytes ( ) )
124
+ . ok_or_else ( || err_unsup_format ! ( "{:?} is not a valid utf-8 string" , os_str) . into ( ) )
125
+ }
126
+
110
127
/// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what
111
128
/// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
112
129
/// to write if `size` is not large enough to fit the contents of `os_string` plus a null
@@ -251,21 +268,4 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
251
268
let os_str = convert_path_separator ( Cow :: Borrowed ( path. as_os_str ( ) ) , & this. tcx . sess . target . target . target_os , Pathconversion :: HostToTarget ) ;
252
269
this. write_os_str_to_wide_str ( & os_str, scalar, size)
253
270
}
254
-
255
- #[ cfg( unix) ]
256
- fn os_str_to_bytes < ' a > ( & mut self , os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
257
- Ok ( os_str. as_bytes ( ) )
258
- }
259
-
260
- #[ cfg( not( unix) ) ]
261
- fn os_str_to_bytes < ' a > ( & mut self , os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
262
- // On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
263
- // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
264
- // valid.
265
- os_str
266
- . to_str ( )
267
- . map ( |s| s. as_bytes ( ) )
268
- . ok_or_else ( || err_unsup_format ! ( "{:?} is not a valid utf-8 string" , os_str) . into ( ) )
269
- }
270
-
271
271
}
0 commit comments