1
Fork 0
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:
Sylvestre Ledru 2021-09-13 08:25:28 +02:00 committed by GitHub
commit fd22feb090
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -229,25 +229,24 @@ fn parse_template<'a>(
pub fn dry_exec(mut tmpdir: PathBuf, prefix: &str, rand: usize, suffix: &str) -> UResult<()> { pub fn dry_exec(mut tmpdir: PathBuf, prefix: &str, rand: usize, suffix: &str) -> UResult<()> {
let len = prefix.len() + suffix.len() + rand; let len = prefix.len() + suffix.len() + rand;
let mut buf = String::with_capacity(len); let mut buf = Vec::with_capacity(len);
buf.push_str(prefix); buf.extend(prefix.as_bytes());
buf.extend(iter::repeat('X').take(rand)); buf.extend(iter::repeat(b'X').take(rand));
buf.push_str(suffix); buf.extend(suffix.as_bytes());
// Randomize. // Randomize.
unsafe { let bytes = &mut buf[prefix.len()..prefix.len() + rand];
// We guarantee utf8. rand::thread_rng().fill(bytes);
let bytes = &mut buf.as_mut_vec()[prefix.len()..prefix.len() + rand]; for byte in bytes.iter_mut() {
rand::thread_rng().fill(bytes); *byte = match *byte % 62 {
for byte in bytes.iter_mut() { v @ 0..=9 => (v + b'0'),
*byte = match *byte % 62 { v @ 10..=35 => (v - 10 + b'a'),
v @ 0..=9 => (v + b'0'), v @ 36..=61 => (v - 36 + b'A'),
v @ 10..=35 => (v - 10 + b'a'), _ => unreachable!(),
v @ 36..=61 => (v - 36 + b'A'),
_ => unreachable!(),
}
} }
} }
// We guarantee utf8.
let buf = String::from_utf8(buf).unwrap();
tmpdir.push(buf); tmpdir.push(buf);
println_verbatim(tmpdir).map_err_context(|| "failed to print directory name".to_owned()) println_verbatim(tmpdir).map_err_context(|| "failed to print directory name".to_owned())
} }