@@ -353,36 +353,33 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
353
353
354
354
fn write_os_str_to_c_string ( & mut self , os_str : & OsStr , ptr : Pointer < Tag > , size : u64 ) -> InterpResult < ' tcx > {
355
355
let bytes = os_str_to_bytes ( os_str) ?;
356
+ let len = bytes. len ( ) ;
356
357
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
357
358
// terminator to memory using the `ptr` pointer would cause an overflow.
358
- if ( bytes . len ( ) as u64 ) < size {
359
+ if ( len as u64 ) < size {
359
360
let this = self . eval_context_mut ( ) ;
360
361
let tcx = & { this. tcx . tcx } ;
362
+ let buffer = this. memory . get_mut ( ptr. alloc_id ) ?. get_bytes_mut ( tcx, ptr, Size :: from_bytes ( len as u64 + 1 ) ) ?;
363
+ buffer[ ..len] . copy_from_slice ( bytes) ;
361
364
// This is ok because the buffer was strictly larger than `bytes`, so after adding the
362
365
// null terminator, the buffer size is larger or equal to `bytes.len()`, meaning that
363
366
// `bytes` actually fit inside tbe buffer.
364
- this. memory
365
- . get_mut ( ptr. alloc_id ) ?
366
- . write_bytes ( tcx, ptr, & bytes) ?;
367
- // We write the `/0` terminator
368
- let tail_ptr = ptr. offset ( Size :: from_bytes ( bytes. len ( ) as u64 + 1 ) , this) ?;
369
- this. memory
370
- . get_mut ( ptr. alloc_id ) ?
371
- . write_bytes ( tcx, tail_ptr, b"0" )
367
+ buffer[ len] = 0 ;
368
+ Ok ( ( ) )
372
369
} else {
373
370
throw_unsup_format ! ( "OsString is larger than destination" )
374
371
}
375
372
}
376
373
}
377
374
378
375
#[ cfg( target_os = "unix" ) ]
379
- fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
380
- Ok ( std:: os:: unix:: ffi:: OsStringExt :: from_bytes ( bytes ) )
376
+ fn os_str_to_bytes < ' tcx , ' a > ( os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
377
+ std:: os:: unix:: ffi:: OsStringExt :: into_bytes ( os_str )
381
378
}
382
379
383
380
#[ cfg( target_os = "unix" ) ]
384
- fn os_str_to_bytes < ' tcx , ' a > ( os_str : & ' a OsStr ) -> InterpResult < ' tcx , & ' a [ u8 ] > {
385
- std:: os:: unix:: ffi:: OsStringExt :: into_bytes ( os_str )
381
+ fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
382
+ Ok ( std:: os:: unix:: ffi:: OsStringExt :: from_bytes ( bytes ) )
386
383
}
387
384
388
385
// On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
0 commit comments