Skip to content

Commit e1e70e9

Browse files
committed
Fix a TOCTOU occurence
1 parent 031ca67 commit e1e70e9

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

build_system/prepare.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use std::process::{Command, Stdio};
66
use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC};
77
use super::path::{Dirs, RelPath};
88
use super::rustc_info::{get_default_sysroot, get_rustc_version};
9-
use super::utils::{copy_dir_recursively, git_command, retry_spawn_and_wait, spawn_and_wait};
9+
use super::utils::{
10+
copy_dir_recursively, git_command, remove_file_if_exists, retry_spawn_and_wait, spawn_and_wait,
11+
};
1012

1113
pub(crate) fn prepare(dirs: &Dirs, vendor: bool) {
1214
RelPath::DOWNLOAD.ensure_fresh(dirs);
1315

14-
if Path::new(".cargo/config.toml").exists() {
15-
std::fs::remove_file(".cargo/config.toml").unwrap();
16-
}
16+
remove_file_if_exists(Path::new(".cargo/config.toml"));
1717

1818
let mut cargo_workspaces = vec![super::build_backend::CG_CLIF.manifest_path(dirs)];
1919

build_system/utils.rs

+8
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ pub(crate) fn spawn_and_wait_with_input(mut cmd: Command, input: String) -> Stri
237237
String::from_utf8(output.stdout).unwrap()
238238
}
239239

240+
pub(crate) fn remove_file_if_exists(path: &Path) {
241+
match fs::remove_file(&path) {
242+
Ok(()) => {}
243+
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
244+
Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()),
245+
}
246+
}
247+
240248
pub(crate) fn remove_dir_if_exists(path: &Path) {
241249
match fs::remove_dir_all(&path) {
242250
Ok(()) => {}

0 commit comments

Comments
 (0)