1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

mktemp: Do not use unsafe

This commit is contained in:
Jan Verbeek 2021-08-27 18:19:50 +02:00
parent 238bcb9634
commit 3e0b3c93ef

View file

@ -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())
}