@@ -448,31 +448,30 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
448
448
}
449
449
450
450
fn read_os_str_from_target_str < ' a > ( & ' a self , scalar : Scalar < Tag > ) -> InterpResult < ' tcx , OsString > {
451
- if cfg ! ( target_os = "unix" ) {
452
- self . read_os_str_from_c_str ( scalar)
453
- } else if cfg ! ( target_os = "windows" ) {
454
- self . read_os_str_from_wide_str ( scalar)
455
- } else {
456
- throw_unsup_format ! ( "support for target OS not yet available" )
451
+ let target_os = self . eval_context_ref ( ) . tcx . sess . target . target . target_os . as_str ( ) ;
452
+ match target_os {
453
+ "linux" => self . read_os_str_from_c_str ( scalar) . map ( |x| x. to_os_string ( ) ) ,
454
+ "windows" => self . read_os_str_from_wide_str ( scalar) ,
455
+ _ => throw_unsup_format ! ( "support for target OS not yet available" ) ,
457
456
}
458
457
}
459
458
460
459
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
461
460
/// the Unix APIs usually handle.
462
- fn read_os_str_from_c_str < ' a > ( & ' a self , scalar : Scalar < Tag > ) -> InterpResult < ' tcx , OsString >
461
+ fn read_os_str_from_c_str < ' a > ( & ' a self , scalar : Scalar < Tag > ) -> InterpResult < ' tcx , & ' a OsStr >
463
462
where
464
463
' tcx : ' a ,
465
464
' mir : ' a ,
466
465
{
467
466
#[ cfg( target_os = "unix" ) ]
468
- fn bytes_to_os_str < ' tcx > ( bytes : & [ u8 ] ) -> InterpResult < ' tcx , OsString > {
469
- Ok ( std:: os:: unix:: ffi:: OsStringExt :: from_bytes ( bytes) . to_os_string ( ) )
467
+ fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
468
+ Ok ( std:: os:: unix:: ffi:: OsStringExt :: from_bytes ( bytes) )
470
469
}
471
470
#[ cfg( not( target_os = "unix" ) ) ]
472
- fn bytes_to_os_str < ' tcx > ( bytes : & [ u8 ] ) -> InterpResult < ' tcx , OsString > {
471
+ fn bytes_to_os_str < ' tcx , ' a > ( bytes : & ' a [ u8 ] ) -> InterpResult < ' tcx , & ' a OsStr > {
473
472
let s = std:: str:: from_utf8 ( bytes)
474
473
. map_err ( |_| err_unsup_format ! ( "{:?} is not a valid utf-8 string" , bytes) ) ?;
475
- Ok ( OsStr :: new ( s) . to_os_string ( ) )
474
+ Ok ( OsStr :: new ( s) )
476
475
}
477
476
478
477
let this = self . eval_context_ref ( ) ;
@@ -509,12 +508,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
509
508
mplace : MPlaceTy < ' tcx , Tag > ,
510
509
size : u64 ,
511
510
) -> InterpResult < ' tcx , ( bool , u64 ) > {
512
- if cfg ! ( target_os = "unix" ) {
513
- self . write_os_str_to_c_str ( os_str, mplace. ptr , size)
514
- } else if cfg ! ( target_os = "windows" ) {
515
- self . write_os_str_to_wide_str ( os_str, mplace, size)
516
- } else {
517
- panic ! ( "support for target OS not yet available" )
511
+ let target_os = self . eval_context_ref ( ) . tcx . sess . target . target . target_os . as_str ( ) ;
512
+ match target_os {
513
+ "linux" => self . write_os_str_to_c_str ( os_str, mplace. ptr , size) ,
514
+ "windows" => self . write_os_str_to_wide_str ( os_str, mplace, size) ,
515
+ _ => panic ! ( "support for target OS not yet available" ) ,
518
516
}
519
517
}
520
518
@@ -574,7 +572,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
574
572
}
575
573
#[ cfg( not( target_os = "windows" ) ) ]
576
574
fn os_str_to_u16vec ( os_str : & OsStr ) -> Vec < u16 > {
577
- os_str. to_str ( ) . encode_utf16 ( ) . collect ( )
575
+ os_str. to_str ( ) . unwrap ( ) . encode_utf16 ( ) . collect ( )
578
576
}
579
577
580
578
let u16_vec = os_str_to_u16vec ( os_str) ;
@@ -589,7 +587,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
589
587
590
588
// Store the UTF-16 string.
591
589
let char_size = Size :: from_bytes ( 2 ) ;
592
- for ( idx, & c) in u16_vec. iter ( ) . enumerate ( ) {
590
+ for ( idx, c) in u16_vec. into_iter ( ) . chain ( iter:: once ( 0x0000 ) ) . enumerate ( ) {
593
591
let place = this. mplace_field ( mplace, idx as u64 ) ?;
594
592
this. write_scalar ( Scalar :: from_uint ( c, char_size) , place. into ( ) ) ?;
595
593
}
@@ -601,12 +599,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
601
599
os_str : & OsStr ,
602
600
memkind : MemoryKind < MiriMemoryKind > ,
603
601
) -> MPlaceTy < ' tcx , Tag > {
604
- if cfg ! ( target_os = "unix" ) {
605
- self . alloc_os_str_as_c_str ( os_str, memkind)
606
- } else if cfg ! ( target_os = "windows" ) {
607
- self . alloc_os_str_as_wide_str ( os_str, memkind)
608
- } else {
609
- panic ! ( "support for target OS not yet available" )
602
+ let target_os = self . eval_context_ref ( ) . tcx . sess . target . target . target_os . as_str ( ) ;
603
+ match target_os {
604
+ "linux" => self . alloc_os_str_as_c_str ( os_str, memkind) ,
605
+ "windows" => self . alloc_os_str_as_wide_str ( os_str, memkind) ,
610
606
}
611
607
}
612
608
0 commit comments