From: nymius <155548262+nymius@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:56:34 +0000 (-0300) Subject: fix(store): replace `Path.exists` by `OpenOptions.create_new` X-Git-Tag: wallet-1.2.0~18^2~3 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/database/scripts/static/enum.AddressIndex.html?a=commitdiff_plain;h=39987882b90b5ddaef3b5ebfc01c5c6e2cb6c0d7;p=bdk fix(store): replace `Path.exists` by `OpenOptions.create_new` `Path.exists` is not safe against time-of-creation, time-of-use (TOCTOU) bugs. `OpenOptions.create_new` on the other hand is atomic, so not prone to this kind of problems. --- diff --git a/crates/file_store/src/store.rs b/crates/file_store/src/store.rs index ec86d4e5..27523f9d 100644 --- a/crates/file_store/src/store.rs +++ b/crates/file_store/src/store.rs @@ -41,16 +41,8 @@ where where P: AsRef, { - if file_path.as_ref().exists() { - // `io::Error` is used instead of a variant on `FileError` because there is already a - // nightly-only `File::create_new` method - return Err(FileError::Io(io::Error::new( - io::ErrorKind::Other, - "file already exists", - ))); - } let mut f = OpenOptions::new() - .create(true) + .create_new(true) .read(true) .write(true) .truncate(true)