From 3f6b009870af551b4caa372547c2ddc9187f6e8b Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sat, 9 Mar 2024 21:32:58 -0600 Subject: [PATCH] Only update env_universal self.last_read_file on success I don't think the existing logic is correct, as the comment says, our internal state is only matched if we *actually* wrote out the file. But if we ran into an error, it doesn't match, does it? --- src/env_universal_common.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/env_universal_common.rs b/src/env_universal_common.rs index fc703474b..75003446e 100644 --- a/src/env_universal_common.rs +++ b/src/env_universal_common.rs @@ -536,23 +536,26 @@ impl EnvUniversal { fn write_to_fd(&mut self, fd: impl AsFd, path: &wstr) -> std::io::Result { let fd = fd.as_fd(); let contents = Self::serialize_with_vars(&self.vars); + let res = write_loop(&fd, &contents); - - if let Err(err) = res.as_ref() { - let error = Errno(err.raw_os_error().unwrap()); - FLOG!( - error, - wgettext_fmt!( - "Unable to write to universal variables file '%ls': %s", - path, - error.to_string() - ), - ); + match res.as_ref() { + Ok(_) => { + // Since we just wrote out this file, it matches our internal state; pretend we read from it. + self.last_read_file = file_id_for_fd(fd.as_raw_fd()); + } + Err(err) => { + let error = Errno(err.raw_os_error().unwrap()); + FLOG!( + error, + wgettext_fmt!( + "Unable to write to universal variables file '%ls': %s", + path, + error.to_string() + ), + ); + } } - // Since we just wrote out this file, it matches our internal state; pretend we read from it. - self.last_read_file = file_id_for_fd(fd.as_raw_fd()); - // We don't close the file. res }