From 260cb78e3a129a4cebffe0f407510b4ccf10b976 Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Sun, 16 Jan 2022 19:07:06 +0100 Subject: [PATCH] Re-write the blob filename creation loop This was written in a way which attempted to avoid easily creating an infinite loop. But really that's a python idiom and doesn't work very well in Rust. Worse, as shown by #2972 it is really easy to still get this wrong. Instead do this the rust way, this way the compiler can also reason properly about the branches and what is unreachable removing some bogus dead code. --- src/blob.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/blob.rs b/src/blob.rs index 239a06ac7..3ba92b73a 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -93,9 +93,11 @@ impl<'a> BlobObject<'a> { stem: &str, ext: &str, ) -> Result<(String, fs::File), BlobError> { - let max_attempt = 15; + const MAX_ATTEMPT: u32 = 16; + let mut attempt = 0; let mut name = format!("{}{}", stem, ext); - for attempt in 1..=max_attempt { + loop { + attempt += 1; let path = dir.join(&name); match fs::OpenOptions::new() .create_new(true) @@ -105,7 +107,7 @@ impl<'a> BlobObject<'a> { { Ok(file) => return Ok((name, file)), Err(err) => { - if attempt == max_attempt { + if attempt >= MAX_ATTEMPT { return Err(BlobError::CreateFailure { blobdir: dir.to_path_buf(), blobname: name, @@ -119,12 +121,6 @@ impl<'a> BlobObject<'a> { } } } - // This is supposed to be unreachable, but the compiler doesn't know. - Err(BlobError::CreateFailure { - blobdir: dir.to_path_buf(), - blobname: name, - cause: std::io::Error::new(std::io::ErrorKind::Other, "supposedly unreachable"), - }) } /// Creates a new blob object with unique name by copying an existing file.