mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Merge pull request #2669 from blyxxyz/mktemp-unsafe
mktemp: Do not use unsafe
This commit is contained in:
commit
fd22feb090
1 changed files with 14 additions and 15 deletions
|
@ -229,15 +229,13 @@ fn parse_template<'a>(
|
|||
|
||||
pub fn dry_exec(mut tmpdir: PathBuf, prefix: &str, rand: usize, suffix: &str) -> UResult<()> {
|
||||
let len = prefix.len() + suffix.len() + rand;
|
||||
let mut buf = String::with_capacity(len);
|
||||
buf.push_str(prefix);
|
||||
buf.extend(iter::repeat('X').take(rand));
|
||||
buf.push_str(suffix);
|
||||
let mut buf = Vec::with_capacity(len);
|
||||
buf.extend(prefix.as_bytes());
|
||||
buf.extend(iter::repeat(b'X').take(rand));
|
||||
buf.extend(suffix.as_bytes());
|
||||
|
||||
// Randomize.
|
||||
unsafe {
|
||||
// We guarantee utf8.
|
||||
let bytes = &mut buf.as_mut_vec()[prefix.len()..prefix.len() + rand];
|
||||
let bytes = &mut buf[prefix.len()..prefix.len() + rand];
|
||||
rand::thread_rng().fill(bytes);
|
||||
for byte in bytes.iter_mut() {
|
||||
*byte = match *byte % 62 {
|
||||
|
@ -247,7 +245,8 @@ pub fn dry_exec(mut tmpdir: PathBuf, prefix: &str, rand: usize, suffix: &str) ->
|
|||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
// We guarantee utf8.
|
||||
let buf = String::from_utf8(buf).unwrap();
|
||||
tmpdir.push(buf);
|
||||
println_verbatim(tmpdir).map_err_context(|| "failed to print directory name".to_owned())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue