From 5d634245a2a6547a7d1dc4a3fcea768c0dff4e20 Mon Sep 17 00:00:00 2001 From: tidely <43219534+tidely@users.noreply.github.com> Date: Thu, 13 Feb 2025 05:55:22 +0200 Subject: [PATCH] remote_server: Remove unnecessary Box, prevent time-of-check time-of-use bug (#24730) The MultiWrite struct is defined in the function scope and is allowed to have a concrete type, which means we can throw away the extra Box. PathBuf::exists is known to be prone to invalid usage. It doesn't take into account permissions errors and just returns false, additionally it introduces a time-of-check time-of-use bug. While extremely unlikely, why not fix it anyway. Release Notes: - remove unnecessary Box - prevent time-of-check time-of-use bug --- crates/remote_server/src/unix.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/remote_server/src/unix.rs b/crates/remote_server/src/unix.rs index 71a770908abe7869c516c0da67567db80283ff66..88ffd1bbf63a2e977acfebed55fb6ea5316ebe1f 100644 --- a/crates/remote_server/src/unix.rs +++ b/crates/remote_server/src/unix.rs @@ -59,7 +59,7 @@ fn init_logging_proxy() { fn init_logging_server(log_file_path: PathBuf) -> Result>> { struct MultiWrite { - file: Box, + file: std::fs::File, channel: Sender>, buffer: Vec, } @@ -80,14 +80,11 @@ fn init_logging_server(log_file_path: PathBuf) -> Result>> { } } - let log_file = Box::new(if log_file_path.exists() { - std::fs::OpenOptions::new() - .append(true) - .open(&log_file_path) - .context("Failed to open log file in append mode")? - } else { - std::fs::File::create(&log_file_path).context("Failed to create log file")? - }); + let log_file = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(&log_file_path) + .context("Failed to open log file in append mode")?; let (tx, rx) = smol::channel::unbounded();