@@ -1738,7 +1738,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
17381738 // Get the raw bytes from the template. This is passed to us from `std` as an `OsString`.
17391739 // These bytes represent a string in _target_ OS encoding, which may or may not match
17401740 // the _host_ OS encoding.
1741- let template_bytes = this. eval_context_ref ( ) . read_c_str ( this. read_pointer ( template_op) ?) ?;
1741+ let template_ptr = this. read_pointer ( template_op) ?;
1742+ let mut template = this. eval_context_ref ( ) . read_c_str ( template_ptr) ?. to_owned ( ) ;
1743+ let template_bytes = template. as_mut_slice ( ) ;
17421744
17431745 // Reject if isolation is enabled.
17441746 if let IsolatedOp :: Reject ( reject_with) = this. machine . isolated_op {
@@ -1804,13 +1806,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
18041806 // Generate a random unique suffix.
18051807 let unique_suffix = SUBSTITUTIONS . choose_multiple ( & mut rng, 6 ) . collect :: < String > ( ) ;
18061808
1807- // The passed in template encoding may or may not match the host.
1808- let mut base = bytes_to_os_str ( & template_bytes[ ..start_pos] ) ?. to_os_string ( ) ;
1809+ // Replace the template string with the random string.
1810+ template_bytes[ start_pos..end_pos] . copy_from_slice ( unique_suffix. as_bytes ( ) ) ;
1811+
1812+ // Write the modified template back to the passed in pointer to maintain POSIX semantics.
1813+ this. write_bytes_ptr ( template_ptr, template_bytes. to_owned ( ) ) ?;
18091814
1810- // Add the randomly generated suffix to the given static prefix .
1811- base . push ( unique_suffix ) ;
1815+ // The passed in template encoding may or may not match the host .
1816+ let p = bytes_to_os_str ( template_bytes ) ? . to_os_string ( ) ;
18121817
1813- let possibly_unique = std:: env:: temp_dir ( ) . join :: < PathBuf > ( base . into ( ) ) ;
1818+ let possibly_unique = std:: env:: temp_dir ( ) . join :: < PathBuf > ( p . into ( ) ) ;
18141819
18151820 let file = fopts. open ( & possibly_unique) ;
18161821
0 commit comments